Skip to main content
gamedev.net
🔒 Locked

Returning Boost Smart Pointers

Started by Antrim Aug 9, 2005 at 4:29 PM 4 replies 500+ views
Original Post
Antrim
Antrim
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
Fruny
Fruny
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Antrim
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?
chollida1
chollida1
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 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
CheersChris
Fruny
Fruny
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Antrim
Antrim
Ahhhh, ok.

Thanks for all the input guys.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.