• Advertisement
Sign in to follow this  

boost::smart_ptr - quick conceptual question

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

I just started looking at using boost in my application. I think there are a few libraries that will simplify my life and my code quite nicely. However, I have a quick conceptual question on boost::smart_ptr. I'm new to writing templates, though I use them by using the STL. Is it not possible to derive a class from boost::smart_ptr? I think I assumed that I could do this, and then when I create a new instance of such a class, I pass pointers around to it. When noone references these pointers they get deleted. But from what I read this doesn't seem to be the case. If I create a boost::smart_ptr to an object, I have to pass the pointer specifically as a smart_ptr, not just a pointer to that object. If that makes sense. Am I completely off base in my understanding? Thanks

Share this post


Link to post
Share on other sites
Advertisement
No, that would be correct. I assume boost::smart_ptr has a similar interface to std::auto_ptr in that it overloads * and -> so you can use it in a limited fashion as a pointer to the object, but if you want to actually pass the pointer to the smart_ptr itself, you would have to do this specifically.

You could do this generically like:

template<class T> void f(boost::smart_ptr<T> *x)
{
// generic operation on x
}

Share this post


Link to post
Share on other sites
Stodge, first of all there is no boost::smart_ptr class. boost::smart_ptr is a collection of smart pointer facilities. Each one with its own purposes. I assume you want to use shared_ptr class. If that is the case, you don't need to derive from it.

If I understand you correctly you want to be able to pass smart pointers to instance of your own classes.

i.e:


class A
{
public:
A(int x):x_(x) {}
void printX()const { cout << x_ << endl; }
private:
int x_;
};

void foo(const boost::shared_ptr<A>& a)
{
a->printX();
}

boost::shared_ptr<A> pA(new A(17)); // 1streference to newed object was created

// print 17
pA->printX();

boost::shared_ptr<A> pA1(pA); // 2 references to same object
foo(pA1);


Which means that you don't need to derive from any class in order to make smart pointers to instances of that class.

As a side note, especially when working with templates it's very common to use typedef in order to avoid long and complex type names. Meaning, if you have a class "Something" and you want to work with smart pointers of that class you could: "typedef boost::shared_ptr<Something> SomethingPtr;"

and now you can work with SomethingPtr, which is much more comfortable, imho :-)

Hope that helps,
Shimon

Share this post


Link to post
Share on other sites
Thanks for the correction.

I was assuming I could do:

void foo(Something* thing)

instead of

void foo(SomethingPtr thing)

The latter is more intrusive, as it involves updating a lot of source files. I guess I don't understand enough about templates and smart pointers yet. This is to be used for network packets, so I may just use a packet pool in my network manager, rather than smart pointers for now.

Thanks again

Share this post


Link to post
Share on other sites
Of course you can do:


void foo(Something* thing)
{
thing->printX();
}
boost::share_ptr<A> pA;
A* a = pA.get();
foo(a);




but then you go back working with dumb pointer and loose the benefit of smart pointers. ;-)

HTH,
Shimon

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement