Pointer in function parameter?
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Meryl Silverburgh Sent: Tuesday, May 08, 2007 9:32 AM To: boost-users@lists.boost.org Subject: [Boost-users] Pointer in function parameter?
vector< boost::shared_ptr<A> > aVector;
aFunc(aVector[0]) ; // just call a
My question is should I change the function parameter of aFuncion from this:
void aFunc(A* aPtr) { // access aPtr }
to this:
void aFunc(shared_ptr<A> aPtr) { // access aPtr }
[Nat] Looks reasonable to me... I believe the docs say that it's conventional to pass shared_ptr by value rather than (e.g.) const reference so that the ref count will be adjusted properly.
On 5/8/07, Nat Goodspeed
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Meryl Silverburgh Sent: Tuesday, May 08, 2007 9:32 AM To: boost-users@lists.boost.org Subject: [Boost-users] Pointer in function parameter?
vector< boost::shared_ptr<A> > aVector;
aFunc(aVector[0]) ; // just call a
My question is should I change the function parameter of aFuncion from this:
void aFunc(A* aPtr) { // access aPtr }
to this:
void aFunc(shared_ptr<A> aPtr) { // access aPtr }
[Nat] Looks reasonable to me... I believe the docs say that it's conventional to pass shared_ptr by value rather than (e.g.) const reference so that the ref count will be adjusted properly.
Thanks. But what if I have void aFunc(const A* aPtr) { // access aPtr } should I change it to void aFunc(const shared_ptr<A> aPtr) { // access aPtr }
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Meryl Silverburgh wrote:
On 5/8/07, Nat Goodspeed
wrote: -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Meryl Silverburgh Sent: Tuesday, May 08, 2007 9:32 AM To: boost-users@lists.boost.org Subject: [Boost-users] Pointer in function parameter?
vector< boost::shared_ptr<A> > aVector;
aFunc(aVector[0]) ; // just call a
My question is should I change the function parameter of aFuncion from this:
void aFunc(A* aPtr) { // access aPtr }
to this:
void aFunc(shared_ptr<A> aPtr) { // access aPtr }
[Nat] Looks reasonable to me... I believe the docs say that it's conventional to pass shared_ptr by value rather than (e.g.) const reference so that the ref count will be adjusted properly.
Thanks. But what if I have
void aFunc(const A* aPtr) { // access aPtr }
should I change it to
void aFunc(const shared_ptr<A> aPtr) { // access aPtr } Hi,
I would say: Yes, you should change it to the 2nd variant, BUT sometimes the first variant is also ok. You could use the 1st variant, but you will loose your benefit from shared_ptr<>. You have to gurantee that the shared_ptr<> is not destroyed while you are running aFunc(). So use the 2nd variant and choose the safe way. I hope it is a little more clear to you now! Much fun Manuel Jung
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Meryl Silverburgh Sent: Tuesday, May 08, 2007 10:41 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Pointer in function parameter?
But what if I have
void aFunc(const A* aPtr) { // access aPtr }
should I change it to
void aFunc(const shared_ptr<A> aPtr) { // access aPtr }
[Nat] 'const A*' is "pointer to const A". 'const shared_ptr<A>' is "const shared_ptr to non-const A". You probably want 'shared_ptr<const A>'.
On 5/8/07, Nat Goodspeed
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Meryl Silverburgh Sent: Tuesday, May 08, 2007 10:41 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Pointer in function parameter?
But what if I have
void aFunc(const A* aPtr) { // access aPtr }
should I change it to
void aFunc(const shared_ptr<A> aPtr) { // access aPtr }
[Nat] 'const A*' is "pointer to const A".
'const shared_ptr<A>' is "const shared_ptr to non-const A".
You probably want 'shared_ptr<const A>'.
Thanks for all the help. So is it safe to say I can replace all 'A*' in my program to shared_ptr<A>? I don't need to worry about scoped_ptr or other smart pointers in boost?
___________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Meryl Silverburgh wrote:
On 5/8/07, Nat Goodspeed
wrote: -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Meryl Silverburgh Sent: Tuesday, May 08, 2007 10:41 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Pointer in function parameter?
But what if I have
void aFunc(const A* aPtr) { // access aPtr }
should I change it to
void aFunc(const shared_ptr<A> aPtr) { // access aPtr }
[Nat] 'const A*' is "pointer to const A".
'const shared_ptr<A>' is "const shared_ptr to non-const A".
You probably want 'shared_ptr<const A>'.
Thanks for all the help. So is it safe to say I can replace all 'A*' in my program to shared_ptr<A>?
I don't need to worry about scoped_ptr or other smart pointers in boost?
Yes, you can. Make sure, you delete the delete(A)s! Manuel Jung
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Meryl Silverburgh Sent: Tuesday, May 08, 2007 11:32 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Pointer in function parameter?
So is it safe to say I can replace all 'A*' in my program to shared_ptr<A>?
[Nat] As long as all your A objects are allocated on the heap, yes.
I don't need to worry about scoped_ptr or other smart pointers in boost?
[Nat] We don't. The only other Boost smart pointer we've used is intrusive_ptr, and that only because we learned a bit late about the shared_ptr mechanism for returning a new shared_ptr to 'this'. Also, with respect to some of the other Boost libraries (notably Boost.Python), shared_ptr is "smarter" than the other Boost smart pointers.
participants (3)
-
Manuel Jung
-
Meryl Silverburgh
-
Nat Goodspeed