2012-04-16 1 views
0

가정하자 개체 :부스트 shared_ptr의 인터페이스/내가 서로 결합하여 사용할 수있다 (2 개)의 방법이있는 API가

boost::shared_ptr<IAsset> getAsset(const std::string & id); 
void update(const Asset & asset); 

및 I는 다른 클래스 boost::shared_ptr<Asset> asset의 멤버 변수가있다. 내가 할 경우

asset = otherObject->getAsset("first_asset"); 

// do some stuff 

otherObject->update(*asset); 

을 나는의 오류를 얻을 :

 \boost\include\boost/smart_ptr/shared_ptr.hpp(378): error C2440: '<function-style-cast>' : cannot convert from 'boost::shared_ptr<T>' to 'boost::shared_ptr<T>' 
     with 
     [ 
      T=IAsset 
     ] 
     and 
     [ 
      T=Asset 
     ] 
     No constructor could take the source type, or constructor overload resolution was ambiguous 
     src\TestMethod.cpp(35) : see reference to function template instantiation 'boost::shared_ptr<T> &boost::shared_ptr<T>::operator =<IAsset>(boost::shared_ptr<IAsset> &&)' being compiled 
     with 
     [ 
      T=Asset 
     ] 
     \boost\include\boost/smart_ptr/shared_ptr.hpp(378): error C2228: left of '.swap' must have class/struct/union 

내가

이 가

누군가가 나에게 올바른 방법을 말할 수 IAssetclass Asset : virtual public IAsset {...} 같이 Asset의 클래스 정의가 확장 않는 점에 유의해야한다 기본 API에 액세스 할 수 없기 때문에이 작업을 수행 할 수 있습니까?

답변

1

귀하의 상속 사용이 올바르지 않다고 생각합니다. updateAsset이 필요한 경우 Asset 또는 그 하위 클래스로 보내야합니다. IAsset은 수퍼 클래스입니다.

1

다운 캐스트가 필요한 IAssetAsset 번으로 캐스팅 했으므로

2

잠재적으로 안전하지 않은 변환이므로 dynamic pointer cast을 사용해야합니다.

(가상 기본 클래스에서 static_cast을 사용할 수 없습니다.)

관련 문제