Search code examples
c++boostinterfaceshared-ptr

boost shared_ptr interface/object


Suppose I have an API that has 2 methods that are to be used in conjunction with one another:

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

and I have a member variable of boost::shared_ptr<Asset> asset in another class. If I do:

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

// do some stuff

otherObject->update( *asset );

I get errors of:

      \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

I should note here that the class definition for Asset does extend IAsset as in class Asset : virtual public IAsset {...}

Can someone tell me the correct way to do this as I have no access to the underlying API?


Solution

  • I think your usage of inheritance is incorrect. If update requires an Asset then you either need to send it an Asset or a subclass thereof. IAsset is a super-class.