Sonoman
Sonoman

Reputation: 3429

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?

Upvotes: 0

Views: 1094

Answers (3)

user1202136
user1202136

Reputation: 11567

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.

Upvotes: 1

CB Bailey
CB Bailey

Reputation: 791949

That's a potentially unsafe conversion so you'll have to use a dynamic pointer cast.

(Note that you can't use a static_cast from a virtual base class.)

Upvotes: 2

Nick
Nick

Reputation: 25799

Because you're casting an IAsset to a Asset which requires a downcast.

Upvotes: 1

Related Questions