Detaching primitive from boost::shared_ptr?

S

Sean

I have a boost::shared_ptr and want to detach the primitive pointer
within it so I can keep using it even after the shared_ptr is
destroyed.

Is there a way to do this?
 
J

Joe Seigh

Sean said:
I have a boost::shared_ptr and want to detach the primitive pointer
within it so I can keep using it even after the shared_ptr is
destroyed.

Is there a way to do this?
Copy the shared_ptr to a local shared_ptr that has the same scope
as the raw pointer.
 
R

Richard Herring

Sean said:
I have a boost::shared_ptr and want to detach the primitive pointer
within it so I can keep using it even after the shared_ptr is
destroyed.

Is there a way to do this?

Why would you want to? The whole point of shared pointers is to avoid
this kind of unsafe manipulation of raw pointers.

If you want to share the pointed-to object, just take a copy of the
shared_ptr in another shared_ptr. That's what the "shared" in the name
means. For as long as there's at least one shared_ptr pointing to it,
the object won't be deleted - *provided* you don't do unsafe tricks with
raw pointers to the same object.
 
A

Alf P. Steinbach

* Sean:
I have a boost::shared_ptr and want to detach the primitive pointer
within it so I can keep using it even after the shared_ptr is
destroyed.

That is not a good idea; can you see why?

Is there a way to do this?

Yes.
 
S

Sean

Agreed. It's not a good idea. However, I still need to do it. Can you
post sample code on how to do it?

thank you.
 
J

Jeff Flinn

Sean said:
Agreed. It's not a good idea. However, I still need to do it. Can you
post sample code on how to do it?

Can you post sample code on why you think you need to do it?

Thanks, Jeff
 
A

Alf P. Steinbach

* Sean:
Agreed. It's not a good idea. However, I still need to do it. Can you
post sample code on how to do it?

Note first that normally you'd just use another shared_ptr to keep a
reference to the original's referred object.

And when that isn't feasible, you'd normally use boost::weak_ptr.

I'm therefore almost certain that what you're trying to do is Very Bad
Design (TM), that you could very easily solve the problem by improving the
design, but if you absolutely want to live extremely dangerously, e.g.

#include <iostream>
#include <ostream>
#include <boost/shared_ptr.hpp>

class X
{
public:
typedef boost::shared_ptr<X> AutoPtr;
static AutoPtr instance(){ return AutoPtr( new X, &destroy ); }
~X(){ std::cout << "Destroyed" << std::endl; }
X* extendedLifetime() { myExtendedLife = true; return this; }
private:
bool myExtendedLife;
X(): myExtendedLife( false ) { std::cout << "Created" << std::endl; }
static void destroy( X* p ) { if( !p->myExtendedLife ){ delete p; } }
};

void doStuff( X::AutoPtr ) {} // Whatever.

X* aUsedNewX()
{
X::AutoPtr p( X::instance() );
doStuff( p );
return p->extendedLifetime();
}

int main()
{
// C-oriented calling code.
X* p = aUsedNewX();
std::cout << "main()" << std::endl;
delete p;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top