470,631 Members | 1,702 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

boost::weak_ptr and shared_ptr pointers from "this"


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
Apr 5 '06 #1
2 3630
In article <e0**********@inews.gazeta.pl>,
Rafa Maj Raf256 <us*******************@raf256.com.invalid> 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.
Apr 5 '06 #2

"Rafal Maj Raf256" <us*******************@raf256.com.invalid> wrote in
message news:e0**********@inews.gazeta.pl...

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
Apr 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Ernst Murnleitner | last post: by
10 posts views Thread by solosnake | last post: by
5 posts views Thread by JKop | last post: by
19 posts views Thread by Daniel Pitts | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.