Jump to content
  • Advertisement
Sign in to follow this  
Antrim

Returning Boost Smart Pointers

This topic is 4848 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 I'm using boost smart pointers, but I'm concerned about portability issues with my code, so I'm wondering what the best practice is for this. In my engine, I have a smart pointer to my script manager and my high scores table , and I also have functions that return pointers to these 2 assets for different game states to utilize. For example, my MenuState would call g_TheGame->GetHighScoreTable() to access the current high scores. Now since the high scores table is located through a boost pointer, I can't just "return HighScoresTable;" unless the MenuState knows and is able to use a boost pointer as well. First, is this even an issue I need to worry about? It seems like relying on states to know and have access to boost could be a portability issue (or maybe I just don't understand what portability issues are properly). If it is something to worry about, it seems like I would want to return the pointer that the smart pointer is holding, but what is the proper way of doing that? Thanks for any input, Antrim

Share this post


Link to post
Share on other sites
Advertisement
Boost is portable. It compiles on just about any platform you are likely to ever use. The concern is more with integration: the code that calls your function must be aware that you are returning a boost::shared_ptr. You can't pass them directly to an API that expects ordinary dumb pointers.

Share this post


Link to post
Share on other sites
right right....integration is the concern, not portability.

So the best practice would actually be to do something like:

HighScoreTable* GetHighScores(){return boost_hs_ptr.get();};


correct?

Share this post


Link to post
Share on other sites
Quote:
Original post by Antrim
right right....integration is the concern, not portability.

So the best practice would actually be to do something like:

HighScoreTable* GetHighScores(){return boost_hs_ptr.get();};


correct?


I'd write it like thsi so you actually return the pointer.

shared_ptr<HighScoreTable*> GetHighScores(){return boostptr; };

Returning a raw pointer after you allready wrapped it is like locking the front door and leaving the backdoor open.

Cheers
Chris

Share this post


Link to post
Share on other sites
Quote:
Original post by Antrim
So the best practice would actually be to do something like:


No, the best practice would be to design your application to use shared_ptr throughout, and to only pass the naked pointer to those external API calls that need it, after making sure it isn't going to keep the pointer stored, or something similar.

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!