Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > boost::weak_ptr and shared_ptr pointers from "this"

Reply
Thread Tools

boost::weak_ptr and shared_ptr pointers from "this"

 
 
=?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=
Guest
Posts: n/a
 
      04-05-2006

How can I manually (other then
http://www.boost.org/libs/smart_ptr/...from_this.html)
create a shared, and a weak pointer to "this"?

Of course I dont want the shared / weak pointer to cause object to be
deleted when shared/weak ptr will go out of scope.

--
Wymiana starych układów... na nowe układy - prawie jak walka z korupcja.
Walka z wychowaniem seksualnym i erotyką - prawie jak walka z patologią.
PiS - prawie jak prawo i sprawiedliwość... Prawie. Prawie robi różnicę.
Myśl. Głosuj rozsądnie. Nie na tanie hasła. // Rafał Maj Raf256
 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      04-05-2006
In article <e0v34k$6gn$(E-Mail Removed)>,
Rafa Maj Raf256 <(E-Mail Removed)> wrote:

> How can I manually (other then
> http://www.boost.org/libs/smart_ptr/...from_this.html)
> create a shared, and a weak pointer to "this"?
>
> Of course I dont want the shared / weak pointer to cause object to be
> deleted when shared/weak ptr will go out of scope.


That's the major problem with shaired pointers IMO. I have a class that
doesn't have this problem. I store the reference counts in a map<void*,
int>

That way, both pointer objects in the code below increment the same
reference count.

class MyClass {
public:
void doThis() {
Ptr<MyClass> p( this );
// ...
}
};

void func( Ptr<MyClass> foo ) {
foo->doThis();
}

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
 
Reply With Quote
 
 
 
 
Joe Gottman
Guest
Posts: n/a
 
      04-06-2006

"Rafal Maj Raf256" <(E-Mail Removed)> wrote in
message news:e0v34k$6gn$(E-Mail Removed)...
>
> How can I manually (other then
> http://www.boost.org/libs/smart_ptr/...from_this.html)
> create a shared, and a weak pointer to "this"?
>
> Of course I dont want the shared / weak pointer to cause object to be
> deleted when shared/weak ptr will go out of scope.
>
>


If you know that your object is long lived, you can do the following:

struct null_deleter
{
template <class T> void operator()(T *) {}
}

Then in your code, just return a shared_ptr<your_type>(this, null_deleter())

Joe Gottman


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
#include <boost/shared_ptr.hpp> or #include "boost/shared_ptr.hpp"? Colin Caughie C++ 1 08-29-2006 02:19 PM
Constness of the container of shared_ptr and the constness of it elements PengYu.UT@gmail.com C++ 14 04-04-2006 01:57 AM
boost::shared_ptr and conversion to base class Ryan Mitchley C++ 6 06-26-2004 06:13 PM
boost::shared_ptr and operator->() Derek C++ 2 12-08-2003 05:13 PM



Advertisments