Jump to content
  • Advertisement
Sign in to follow this  
Whackjack

Boost::Shared_Ptr and *this

This topic is 4213 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

So here's a problem that I've run into. I have a class defined thus:
class Savable
{
    // some pure virtual methods
    virtual void saveYourself(boost::shared_ptr<Saver> saver) = 0;
}
And another class that implements the above:
class SomeClass : Savable
{
    // implements Savable methods
}
Finally, I have a class that writes a Savable to disk:
class Saver
{
    virtual void save(boost::shared_ptr<Savable> saveMe);
}
Now, there might be the case where SomeClass calls Saver because it wants to save itself because the end user has a subclass that writes to disk in it's own special way. Now, I know I can't call Saver::save() like this:
boost::shared_ptr me(this);
saver->save(me);
because the shared_ptr would try to delete *this when it goes out of scope. And I didn't see any way in shared_ptr to release the pointer. What is a good way to work around this?

Share this post


Link to post
Share on other sites
Advertisement
Looks like the null deleter is exactly what I want. The enable_shared_from_this would only work if the class knew that another shared_ptr to *this existed. Thanks for the quick response! Rating++!

Share this post


Link to post
Share on other sites
You're welcome. Still, I do recommend you take a look at the Serialization library. It does pretty much exactly what you are trying to do, and then some.

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!