Jump to content
  • Advertisement
Sign in to follow this  
Black Knight

Integrating boost::shared_ptr into existing project

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

Hi guys.I have some old projects which I would like to return and I want to use boost shared_ptr weak_ptr etc. What is the best way to integrate it into my project? Currently I have classes and there are raw pointers inside classes. Some of these pointers are referenced by other classes. Let me give an example : My MainWindow class has a World* pWorld; And in my ItemEditor class I have a World *pWorld; too which I can set through the constructor or a member function. The main window creates the world ItemEditor just holds a reference.At program termination MainWindow deletes world. How should I handle these kind of situations with boost.I also would like to handle any dangling pointer situations with boost.

Share this post


Link to post
Share on other sites
Advertisement
If you happened to have typedef'd your pointer types then you can just change the typedef declaration, otherwise it is a brute force find and replace.

Share this post


Link to post
Share on other sites
Regex helps. A lot. It would very likely be quicker to learn regex and replace them all with a single command, than it would be to manually replace every pointer one by one.

Share this post


Link to post
Share on other sites
Well I am more curious about when to use shared_ptr when to use weak_ptr etc.The boost docs say shared_ptr is for shared ownership forexample.
Does my example with the main window and the item editor is a case for shared owner ship? Or is the itemeditor just an observer and should it use weak_ptr?
That kind of stuff...

Share this post


Link to post
Share on other sites
If an object A holds a shared_ptr to an object B, then object A affects B's lifetime (i.e., B will not die while A is alive). A weak_ptr doesn't affect an object's lifetime. It can just be used to check if the object it refers to is alive.

In your example, do you want a MainWindow or an ItemEditor object to affect the lifetime of a world object, i.e., should these objects prevent the world object from dying? If the answer is yes, use shared_ptr. If it's no, use weak_ptr.

Share this post


Link to post
Share on other sites
Well in this case I want the world to exists as long as the MainWindow is open when it is closed the application ends so it should be destroyed.Item editor just needs to access items inside the world class so it needs a reference to the world it should not affect the life time of the world.
So I should have a shared_ptr inside MainWindow to World but a weak_ptr to World inside ItemEditor is that correct O_O

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!