Re: Overloaded Constructors

From: Richard Herring (junk_at_[127.0.0.1)
Date: 03/29/04


Date: Mon, 29 Mar 2004 16:48:53 +0100

In message <nAI8c.39188$PY1.733824@bgtnsc05-news.ops.worldnet.att.net>,
Siemel Naran <SiemelNaran@REMOVE.att.net> writes
>"Buster" <noone@nowhere.com> wrote in message
>news:c3ue4s$s2f$1@newsg2.svr.pol.co.uk...
>> >>"Cy Edmunds" wrote
>
>> >>>So why did you use a std::auto_ptr at all?
>> >>
>> >>Good question. Just a style choice to make it clear to the reader of
>the
>> >>header file that this class does in fact own the pointer, and it's not a
>> >>just a pointer to an existing object.
>>
>> [snip]
>>
>> > Your design seems to be technically sound. However, when perusing a
>class
>> > definition I find a pointer to be scary but a std::auto_ptr to be even
>> > scarier. Since you seemed concerned about how your class "reads" (good
>for
>> > you!) you might consider eliminating this unneccesary complication.
>>
>> I vote for keeping the auto_ptr. In this instance it has excellent
>> documentary value, and it _is_ minimal. And it's not scary.
>
>In addition, the use of std::auto_ptr over a raw pointer gives us exception
>safety in the constructor, because if the member initialization list or
>constructor body throws then the destructor is not called and we would not
>delete the raw pointer. We can put try-catch blocks around the various
>constructors, but besides I don't know the notation :).
>
>As for the complication of auto_ptr's copy semantics I guess it could be
>confusing to some.
>
So why does everyone overlook boost::scoped_ptr? Distinct declaration of
ownership, exception-safe constructor, no reference-counting overhead,
and no copy semantics at all.

-- 
Richard Herring


Relevant Pages

  • Re: Inherited Methods and such
    ... Now, what you want is to get in the constructor of some S derived from T, ... I have no problem with the semantics of object ... wrong to call it "dispatch on the operations of T." ... procedure Register; ...
    (comp.lang.ada)
  • Re: Returning by value (here we go again!)
    ... > and as such the copy constructor wouldn't need to be ... This can be achieved with move semantics: ... substr to construct another string. ... into an existing string a temporary is still created. ...
    (comp.lang.cpp)
  • Re: copy ctor question
    ... In order to implement a copy constructor, you must inturn implement copy semantics ... for each member and base class. ... just empty like the default destructor. ...
    (comp.lang.cpp)
  • Re: copy ctor question
    ... > for each member and base class. ... The compiler generated copy constructor isn't ... > do proper copy semantics themselves. ... Secondly you can't put a breakpoint on a compiler generated ...
    (comp.lang.cpp)
  • Re: Assigning a reference to a variable
    ... call to the copy constructor. ... In C++ classes use value semantics by ... simply return a reference to an object and not worry about a call to the ... if you have ref class SomeClass you can create an object using reference ...
    (microsoft.public.dotnet.languages.csharp)