Boost logo

Boost Users :

From: Andrew Holden (aholden_at_[hidden])
Date: 2006-11-20 13:31:10


Hughes, James wrote:

> > void MyClass::init()
> > {
> > // either
> > spOther = shared_ptr<someOtherClass>(new someOtherClass(this));
> or
> > spOther.reset(new someOtherClass(this)); }
> >
> > Dave.
> > --
> > Dave Slutzkin
> > Melbourne, Australia
> > daveslutzkin_at_[hidden]
>
> Another beginners question...
>
> Are the two ways of doing this, as shown above, exactly the same? i.e.
> execute the same code and are therefore at the same cost, or is one
more
> efficient than the other?
>

Reset should be slightly more efficient. The first one, constructs a
new shared_ptr, assigns it to your variable, and then destroys it. The
copy and destruction involve tweaking the reference counts (increment,
then decrement).

That said, the difference in performance will be statistically
insignificant, especially after compiler optimizations. Use whichever
makes more sense to you.


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