Jump to content
  • Advertisement
Sign in to follow this  
sprite_hound

boost::shared_ptrBase to boost::shared_ptrDerived

This topic is 3623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is something like this possible? (I'm leaning towards not, but I just thought I'd make sure...) The code:
class Base {
};
class Derived : public Base {
};

int main(int argc, char* argv[]) {
	boost::shared_ptr<Base> b(new Derived);
	
	boost::shared_ptr<Derived> d(b); // line 25
}


With compiler errors for it:
C:\code_utils\boost-1_34_1\boost\shared_ptr.hpp||In constructor `boost::shared_ptr<T>::shared_ptr(const boost::shared_ptr<Y>&) [with Y = Base, T = Derived]':|
C:\Documents and Settings\Matthew\My Documents\My C++ Projects\testing\toroidalarray\toroidalarray.cpp|25|instantiated from here|
C:\code_utils\boost-1_34_1\boost\shared_ptr.hpp|195|error: invalid conversion from `Base* const' to `Derived*'|
||=== Build finished: 1 errors, 0 warnings ===|


Share this post


Link to post
Share on other sites
Advertisement
Why not the other way around?

class Base {
};
class Derived : public Base {
};

int main(int argc, char* argv[]) {
boost::shared_ptr<Derived> d(new Derived);

boost::shared_ptr<Base> b(d);
}



Works like a charm...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!