#include <iostream>
using namespace std;
class Exercise 
 int a;
 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 ?????

12 Years
Discussion Span
Last Post by WolfPack

Caught the error ....
Missed const in Exercise& operator= (const Exercise &x);

But again why VC++ was not throwing error.


I don't think there is a reason for GCC to give an error because of the missing const . The assignment operator of Class C can have any parameter of C, C&, const C&, volatile C&, or const volatile C&. .

It ought to be something else. But I dont know what. Maybe GCC is guarding against self-assignment (which is possible if const is not used) while VC++ is not.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.