Skip to main content
GameDev.net gamedev.net
🔒 Locked

const boost::shared_ptr?

Started by hymerman Mar 11, 2007 at 5:27 PM 3 replies 12.7k views
Original Post
hymerman
hymerman
Hi, just a quick couple of questions: Firstly, if I want to return a shared_ptr to a thing I don't want modified, can I do it by returning a "const shared_ptr"? I know I can do it with normal pointers and references but will the reference counting stuff cock this up if I go copying it around and stuff? Secondly, why won't this work? I don't know a lot about much, so forgive me if it's blindingly obvious ;)

class Something {
public:
    std::vector<const boost::shared_ptr<SomeType>> getThings() {
        return mThings;
    }
private:
    std::vector<boost::shared_ptr<SomeType>> mThings;
};

I'm guessing that since the vector is templated on something non-const, it can't return it as const since that's a different type which can't be casted implicitly. What should I be writing instead if I want to return a vector of pointers to things I don't want changed? Cheers :)
Sneftel
Sneftel
You can return a boost::shared_ptr.
MaulingMonkey
MaulingMonkey
1) You've also placed the const in the wrong place --- const boost::shared_ptr would just prevent the pointer from being changed to point elsewhere (which, since this will just be copied anyways, is useless) -- the following example compiles fine, even though bar() is not a const function:

#include <boost/shared_ptr.hpp>#include <vector>struct foo { void bar() {} };int main() {	const boost::shared_ptr< foo > f( new foo );	f->bar();}


What you want is boost::shared_ptr< const SomeType >.

2.a) Because >> is interpreted as operator>> rather than two template argument list closures.

2.b) Because std::vector< T > and std::vector< const T > are completely different types as far as the compiler is concerned, and it provides no conversion operators to make this possible for you. You can, however, use an alternative initialization method, such as an iterator range:

class Something {public:    std::vector< boost::shared_ptr< const SomeType > > getThings() {        return std::vector< boost::shared_ptr< const SomeType > >( mThings.begin() , mThings.end() );    }private:    std::vector< boost::shared_ptr< SomeType > > mThings;};
hymerman
hymerman
Thanks very much you two, that's helped a lot :)

One more simple question:

If I'm using one of these get methods that returns a "boost::shared_ptr", will I have to write code like this:

boost::shared_ptr<const Type> getPointer();boost::shared_ptr<const Type> ptr = getPointer();// Rather than:boost::shared_ptr<Type> ptr2 = getPointer();


I'm guessing a resounding 'yes'. I see what people mean when they say const correctness spreads like a virus :)
Sneftel
Sneftel
Yep, pretty much... though I would say that it's lack of const-correctness that spreads like a virus (especially if you screw it up right from the beginning). The code you posted turns out not to happen often, though, since usually you'll be using the returned pointer for something rather than storing it somewhere.

Topic Locked

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

Sign in to reply to this topic.