Copy constructor with non const rhs arg

Tony Wetmore tony.wetmore@solipsys.com
Wed May 12 16:43:00 GMT 2004


(I sent this earlier to Paul but forgot to CC the list...)

I believe the problem here is that "test dd = foo()" is using a
temporary object (returned by foo).  You cannot pass a non-const
reference to that temporary object into test's copy constructor.
Temporary objects are always "const".

You can, of course, pass a const reference to a temporary object.

Similarly, if foo() returns a reference, that reference must point to a
valid (not temporary) object, so passing a reference to THAT object into
test's copy constructor is just fine.

So I think gcc's behavior is correct (though the error text is a bit
unclear).  I don't know what Microsoft's compiler is doing in that case,
but it seems unusual and could probably lead to bad behavior.

---
Tony Wetmore
Raytheon Solipsys
mailto:tony.wetmore@solipsys.com
http://www.solipsys.com
 
 


-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Eljay Love-Jensen
Sent: Wednesday, May 12, 2004 12:05 PM
To: paul moore; gcc-help@gcc.gnu.org
Subject: Re: Copy constructor with non const rhs arg


Hi Paul,

I believe a copy constructor requires a const parameter.

But I may be mistaken.  I've been mistaken before.

You can make the auto_ptr member variable mutable.

--Eljay




More information about the Gcc-help mailing list