This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Copy constructor does not accept parameter by value
- To: "Arun Saini , Gurgaon" <arun at ggn dot hcltech dot com>
- Subject: Re: Copy constructor does not accept parameter by value
- From: "Artem Khodush" <artem at duma dot gov dot ru>
- Date: Wed, 13 Jun 2001 20:16:08 +0400
- >Received: from artem by mailsrv.duma.gov.ru (SMI-8.6/SMI-SVR4)id UAA12047; Wed, 13 Jun 2001 20:19:09 +0400
- Cc: <gcc at gcc dot gnu dot org>
- References: <5F0021EEA434D511BE7300D0B7B6AB533E7730@MAIL2>
Arun Saini , Gurgaon wrote:
> I am reproducing a portion of code and the error reported by the compiler
> when I try to compile it.
>
>
> Test someFunc(void)
> {
> Test tempTest;
> tempTest.testVar = 10;
>
> return tempTest;
> }
>
>
> int main()
> {
> Test t1;
> t1 = someFunc();
> return 0;
> }
>
This is not a bug. Here what's going on:
the compiler created a temporary variable to store
the result of someFunc(), and you are trying to pass
that temporary to a copy constructor taking Test&
(without a const), which is not allowed in c++.
You have two options: either make Test::Test
copy constructor to take const Test& parameter,
or use the technique invented for std::auto_ptr
to overcome this very problem (which is better
discussed elsewhere).