2012-04-16 8 views
0

オブジェクトIが互いに関連して使用される2つの方法有するAPIがあると仮定:ブーストshared_ptrのインタフェースは、/

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

を、私は別のクラスに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 

を私はAssetのためのクラス定義は、誰かが私に正しい方法を伝えることができclass Asset : virtual public IAsset {...}

のようIAssetを拡張しないことをここで注意してください私は基礎となるAPIへのアクセス権がないので、これを行うには?

答えて

1

私はあなたの継承の使用が間違っていると思います。 updateAssetが必要な場合は、Assetまたはそのサブクラスを送信する必要があります。 IAssetはスーパークラスです。

1

IAssetをキャストしているので、ダウンキャストが必要なAssetになります。

2

これは安全でない可能性がある変換ですので、dynamic pointer castを使用する必要があります。

static_castは仮想基本クラスから使用できません)

関連する問題