This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Copy constructor does not accept parameter by value


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).




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]