|
Boost Users : |
From: Spam-Receiver (spam-receiver_at_[hidden])
Date: 2008-04-25 03:21:30
Hi Lex!
shared_ptr<Foo> bla; // Creates an empty shared_ptr
object
bla = shared_ptr<Foo>(new Foo(fooInstance)); // Creates a temporary
shared_ptr object,
// which owns the copy of
fooInstance,
// then assigns this temporary
object to bla.
// Now both bla and the
temporary object own
// the fooInstance-copy.
...; // Afterwards the temporary
shared_ptr object
// is not needed anymore and
is deleted.
// Now only bla owns the
fooInstance-copy.
// The exact moment, when the
temporary object
// is destroyed, depends on
your compiler toolset.
Regards,
T.
-----Ursprüngliche Nachricht-----
Von: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] Im Auftrag von Lex Fridman
Gesendet: Freitag, 25. April 2008 04:25
An: boost-users_at_[hidden]
Betreff: Re: [Boost-users] shared_ptr Initial Assignment
Hi Richard,
That was foolish of me to think that bla will point to fooInstance.
Obviously it will point to a copy of Foo instance. Thanks for that note.
However, you also mentioned that the two options are different:
(1) shared_ptr<Foo> bla(new Foo(fooInstance));
(2)
shared_ptr<Foo> bla;
bla = shared_ptr<Foo>(new Foo(fooInstance));
You mentioned that the second will create a temporary that will "go
away". Can you elaborate on that. It seems to me that it shouldn't go
away, as long as bla is within scope. I guess that was my main question
originally, whether the second option is a correct way to set bla (when,
say, it's a class member).
Many thanks!
Lex
_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net