#include <iostream>
using namespace std;
class Exercise
{
public:
int a;
Exercise(){cout<<"constructor\n";}
Exercise(const Exercise& x)
{
cout<<"copy constructor\n";
a = x.a;
}
Exercise& operator= (Exercise &x)
{
a = x.a;
cout<<"assignment operaor\n";
return *this;
}
};
Exercise fun(Exercise& );
int main(void)
{
Exercise y;
Exercise z;
z = fun(y);
return 0;
}
Exercise fun(Exercise& z)
{
return z;
}
The above piece of code was compiled using both VC++ and gcc compiler. VC gave me the intended output, but on gcc i got some error.
constructor.cpp: In function `int main()':
constructor.cpp:30: initialization of non-const reference type `class Exercise &
'
constructor.cpp:30: from rvalue of type `Exercise'
constructor.cpp:17: in passing argument 1 of `Exercise::operator =(Exercise &)'
What is this error ?????