Re: C++ question related to casting

From: Nick Lewycky <nicholas@dont-contact.us>
Date: Tue, 23 May 2006 15:02:19 -0400

Henrik Nordstrom wrote:
> tis 2006-05-23 klockan 13:22 -0400 skrev Nick Lewycky:
>
>
>>It's a temporary. pe->getReply()->head will get casted into the
>>HttpHeader, used to call putStr(...), then its life is over, at which
>>point the C++ implementation is free to destroy it at its leisure (some
>>impl's do it after the end of the statement, some wait until the end of
>>the function).
>>
>>Even if you'd said "HttpHeader HH = ...", it would still create a
>>temporary, copy-construct the new HttpHeader, and then destroy the
>>temporary.
>
>
> So why did the original get smashed when the temporary was destructed? A
> missing copy constructor?

Because the temporaries destructor called "clean()" which freed every
element in the entries vector. Realize that the copy was shallow, not
deep, so pointers stored in the class were duplicated, not copied properly.

If possible, it should store the HttpHeaderEntry objects themselves
instead of pointers to them. Done properly, there shouldn't be any need
for a destructor at all.

If not possible, then HttpHeader should either have an explicit copy
constructor which does the right thing, or else be non-copyable by
adding unimplemented private copy constructor and operator= . See
http://www.boost.org/boost/noncopyable.hpp for the technique.

Nick
Received on Tue May 23 2006 - 13:02:22 MDT

This archive was generated by hypermail pre-2.1.9 : Thu Jun 01 2006 - 12:00:04 MDT