Archived

This topic is now archived and is closed to further replies.

gimp

[boost] Using smart pointers from boost...

Recommended Posts

All these smart pointers seem to be making life easier in many ways but making it harder in others. Does anyone have any idea''s about the following problems (like how I''m probably doing it wrong). After construction of an object I want to ''new'' an object and ''give'' it to a smart pointer to manage. the member declaration variable looks like this: boost::shared_ptr m_Parser; Later I want to ''give'' it the SAX parser. I do it like this: m_Parser.reset(new SAXParser); This just looks plain ugly, resetting a smart pointer that has never been assigned. Is there a better way? Second question. If I want to have an array of say, CFiles. I want to ''new'' a CFile then .push_back(TheFile) it in to a std::vector. My ugly code would be .push_back(boost::shared_ptr(FileName)). Is that correct, or an I implicitly calling a copy constructor in there somewhere? Third question, I tried to use a weak pointer to a scoped pointer last night but the compiler said that I had no -> operator on the weak pointer. How do I actually use it to get at the class once it''s taken a copy of the scoped ptr? Many thanks, Chris Chris Brodie http:\\fourth.flipcode.com

Share this post


Link to post
Share on other sites
quote:
Original post by gimp
All these smart pointers seem to be making life easier in many ways but making it harder in others. Does anyone have any idea''s about the following problems (like how I''m probably doing it wrong).

After construction of an object I want to ''new'' an object and ''give'' it to a smart pointer to manage. the member declaration variable looks like this:

boost::shared_ptr m_Parser;



Later I want to ''give'' it the SAX parser. I do it like this:

m_Parser.reset(new SAXParser);

This just looks plain ugly, resetting a smart pointer that has never been assigned. Is there a better way?



I think you are just being picky here . But since you don''t seem to use the sptr before, why don''t you just construct it when you have to new?

quote:

Second question. If I want to have an array of say, CFiles. I want to ''new'' a CFile then .push_back(TheFile) it in to a std::vector. My ugly code would be .push_back(boost::shared_ptr(FileName)). Is that correct, or an I implicitly calling a copy constructor in there somewhere?



Yes you are. But whatever you do, there will always be a call to a copy constructor. Don''t worry about. It is very lightweight. And the advantage of storing shared pointers far outweight the overhead of the of the copy constructor.

quote:

Third question, I tried to use a weak pointer to a scoped pointer last night but the compiler said that I had no -> operator on the weak pointer. How do I actually use it to get at the class once it''s taken a copy of the scoped ptr?




Sorry. I don''t know. It should work with ->. Can you show the piece of code.

Share this post


Link to post
Share on other sites
gimp:
1. if you don''t like reset(), then #define reset init ... and hey! don''t do this.

2. please remember that the vector is std::vector< boost::share_ptr<CFile> > if you want to push back a share_ptr<CFile>.

3. hmm.. if it is scope_ptr<char>, then of course, it don''t have the -> operator (I know you knew that )... err... please post part of your code. Try using std::auto_ptr and see if the error still there. They should be the same.

Share this post


Link to post
Share on other sites
I think you could write

m_Parser = shared_ptr(new SAXParser);

this is IMO a little less ugly (especially if you typedef your shared_ptr).

And... There may be a possibility that you could overload the new operator in class SAXParser to return a shared_ptr. It''s just an idea; I don''t know if it''s really possible (and it may not be such a good idea... All kinds of problems come to mind).

But anyway, it would allow you to write the really sexy:

m_Parser = new SAXParser;

Now that''s sexy. You could also create a function NewSAXParser that does just that...

Cédric

Share this post


Link to post
Share on other sites
quote:
Original post by gimp
Later I want to ''give'' it the SAX parser. I do it like this:

m_Parser.reset(new SAXParser);

This just looks plain ugly, resetting a smart pointer that has never been assigned. Is there a better way?



No problem. It''s the least expensive way to reassign the held resource in any smart pointer. You could do use assignment too

m_Parser = shared_ptr( new m_Parser() )

but that incurs another copy of shared_ptr. Use reset all the way.

OT: Making the reset function as a member is somewhat a mistake. It should be a non member function instead. Because the class held by the smart pointer may also have a function called reset(), then


m_Parser.reset( Blah );
m_Parser->reset( Blah );


does quite different things but not immediately apparent during code reviews.

quote:

Second question. If I want to have an array of say, CFiles. I want to ''new'' a CFile then .push_back(TheFile) it in to a std::vector. My ugly code would be .push_back(boost::shared_ptr(FileName)). Is that correct, or an I implicitly calling a copy constructor in there somewhere?



It is a problem with STL containers that you cannot initialize an entry directly. But since smart pointers typically store a pointer (+ ref counter for shared_ptr), it is a cheap class to copy.

quote:

Third question, I tried to use a weak pointer to a scoped pointer last night but the compiler said that I had no -> operator on the weak pointer. How do I actually use it to get at the class once it''s taken a copy of the scoped ptr?



Do you want to take a pointer of the smart pointer or the pointer inside the smart pointer (what a mouthful) ?

Share this post


Link to post
Share on other sites
Thanks for the help here guys. I havent revisited that weak_ptr code yet so I''ll have to defer that question.

The only remaining problem i have is that now my memory debugger is broken It see''s the ''new'' take place but not the delete as it''s hidden away in the smart pointer''s dtor. So, in theory I ''guess'' I no longer have leaks

Chris Brodie
http:\\fourth.flipcode.com

Share this post


Link to post
Share on other sites