By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,501 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,501 IT Pros & Developers. It's quick & easy.

shared_ptr + this

P: n/a
Hi,

I have a question regarding boost::shared_ptr. I want to let
shared_ptr take care of my objects' lifetimes, so I change
all my function signatures so that they accept shared_ptrs
instead of raw pointers.

I wonder how to deal with cases where a this pointer is
passed as an argument. Is it ok to write something like the
following?

void foo::bar()
{
add_to_collection(boost::shared_ptr<foo>(this));
}

int main()
{
boost::shared_ptr<foo> f(new foo());
foo->bar();
}

I'm asking because shared_ptr is non-intrusive and I fear
that the above code could create a second counter for an
object that is already reference counted.

Any comments?
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
ralpe wrote:
....
I'm asking because shared_ptr is non-intrusive and I fear
that the above code could create a second counter for an
object that is already reference counted.

Any comments?


Use intrusive pointers.

Jul 22 '05 #2

P: n/a
In message <42**************************@posting.google.com >, ralpe
<ra************@gmx.net> writes
Hi,

I have a question regarding boost::shared_ptr. I want to let
shared_ptr take care of my objects' lifetimes, so I change
all my function signatures so that they accept shared_ptrs
instead of raw pointers.

I wonder how to deal with cases where a this pointer is
passed as an argument. Is it ok to write something like the
following?

void foo::bar()
{
add_to_collection(boost::shared_ptr<foo>(this));
}

int main()
{
boost::shared_ptr<foo> f(new foo());
foo->bar();
}

I'm asking because shared_ptr is non-intrusive and I fear
that the above code could create a second counter for an
object that is already reference counted.

Any comments?


You could have a boost::weak_pointer member initialised from this, and
then use boost::make_shared to create a shared_ptr from it to pass to
add_to_collection. Using the weak_ptr will ensure that all shared_ptrs
created from it will share the same reference count.
--
Richard Herring
Jul 22 '05 #3

P: n/a
On 22 Aug 2004 22:58:55 -0700, ra************@gmx.net (ralpe) wrote:
Hi,

I have a question regarding boost::shared_ptr. I want to let
shared_ptr take care of my objects' lifetimes, so I change
all my function signatures so that they accept shared_ptrs
instead of raw pointers.

I wonder how to deal with cases where a this pointer is
passed as an argument. Is it ok to write something like the
following?

void foo::bar()
{
add_to_collection(boost::shared_ptr<foo>(this));
}

int main()
{
boost::shared_ptr<foo> f(new foo());
foo->bar();
}

I'm asking because shared_ptr is non-intrusive and I fear
that the above code could create a second counter for an
object that is already reference counted.

Any comments?


http://www.boost.org/libs/smart_ptr/...html#from_this

Tom
Jul 22 '05 #4

P: n/a
Gianni Mariani <gi*******@mariani.ws> wrote in message news:<cg*******@dispatch.concentric.net>...
ralpe wrote:
...
I'm asking because shared_ptr is non-intrusive and I fear
that the above code could create a second counter for an
object that is already reference counted.

Any comments?


Use intrusive pointers.


I chose shared_ptr because it is going to be standardized.
Will there be an intrusive pointer in the next standard?
Jul 22 '05 #5

P: n/a
Richard Herring <ju**@[127.0.0.1]> wrote in message news:<VE**************@baesystems.com>...
In message <42**************************@posting.google.com >, ralpe
<ra************@gmx.net> writes
Hi,

I have a question regarding boost::shared_ptr. I want to let
shared_ptr take care of my objects' lifetimes, so I change
all my function signatures so that they accept shared_ptrs
instead of raw pointers.

I wonder how to deal with cases where a this pointer is
passed as an argument. Is it ok to write something like the
following?

void foo::bar()
{
add_to_collection(boost::shared_ptr<foo>(this));
}

int main()
{
boost::shared_ptr<foo> f(new foo());
foo->bar();
}

I'm asking because shared_ptr is non-intrusive and I fear
that the above code could create a second counter for an
object that is already reference counted.

Any comments?


You could have a boost::weak_pointer member initialised from this, and
then use boost::make_shared to create a shared_ptr from it to pass to
add_to_collection. Using the weak_ptr will ensure that all shared_ptrs
created from it will share the same reference count.


Good idea. Thank you.

Will weak_ptr be part of the next standard?
I only read about shared_ptr.
Jul 22 '05 #6

P: n/a
Richard Herring <ju**@[127.0.0.1]> wrote:
In message <42**************************@posting.google.com >, ralpe
<ra************@gmx.net> writes
Hi,

I have a question regarding boost::shared_ptr. I want to let
shared_ptr take care of my objects' lifetimes, so I change
all my function signatures so that they accept shared_ptrs
instead of raw pointers.

I wonder how to deal with cases where a this pointer is
passed as an argument. Is it ok to write something like the
following?

<snip>

I'm asking because shared_ptr is non-intrusive and I fear
that the above code could create a second counter for an
object that is already reference counted.

Any comments?

It can indeed.

You could have a boost::weak_pointer member initialised from this, and
then use boost::make_shared to create a shared_ptr from it to pass to
add_to_collection. Using the weak_ptr will ensure that all shared_ptrs
created from it will share the same reference count.


This can be automated by deriving your classes from boost::enable_shared_from_this.

See:
http://www.boost.org/libs/smart_ptr/...from_this.html
Jul 22 '05 #7

P: n/a
In message <ea************************@posting.google.com>, Simon Turner
<s_********@yahoo.co.uk> writes
Richard Herring <ju**@[127.0.0.1]> wrote:
In message <42**************************@posting.google.com >, ralpe
<ra************@gmx.net> writes
>Hi,
>
>I have a question regarding boost::shared_ptr. I want to let
>shared_ptr take care of my objects' lifetimes, so I change
>all my function signatures so that they accept shared_ptrs
>instead of raw pointers.
>
>I wonder how to deal with cases where a this pointer is
>passed as an argument. Is it ok to write something like the
>following?
>
<snip>
>
>I'm asking because shared_ptr is non-intrusive and I fear
>that the above code could create a second counter for an
>object that is already reference counted.
>
>Any comments?


It can indeed.

You could have a boost::weak_pointer member initialised from this, and
then use boost::make_shared to create a shared_ptr from it to pass to
add_to_collection. Using the weak_ptr will ensure that all shared_ptrs
created from it will share the same reference count.


This can be automated by deriving your classes from
boost::enable_shared_from_this.

See:
http://www.boost.org/libs/smart_ptr/...from_this.html

Better still! (enable_... is about two versions of Boost more recent
than my compiler can cope with, so I haven't yet been able to use it
myself :-( )

--
Richard Herring
Jul 22 '05 #8

P: n/a
I know that file functions (see below) are non-standard,
but there must be a 'sub'standard as this are very common
things.

I want to:
get a list of files in a directory,
get the size and access mode of a file, etc.

I am using g++

Thanks,
marc

Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.