#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 ?????

Dani AI

Generated

The error is caused by a temporary (the value returned from fun) being used where your assignment operator expects a non-const lvalue reference. Standard C++ does not allow binding an rvalue/temporary to a non-const lvalue reference; GCC enforces this rule and reports the error. See the C++ reference rules on reference initialization for the precise wording and examples: Reference initialization (cppreference).

What happened on VC++ is a historical nonstandard extension: older MSVC versions permitted binding a temporary to a non-const lvalue reference (often emitting warning C4239 instead of an error). That made the program compile on MSVC but the code was not strictly conforming. Microsoft documents this kind of compiler behavior in their warnings list: Compiler warning C4239 (learn.microsoft.com).

already found the practical fix (make the assignment take a const reference). For portability and modern correctness consider these points:

  • Follow the standard: make copy-assignment take a const reference (and return *this).
  • In C++11 and later, add a move-assignment overload to efficiently handle rvalues.
  • Enable strict warnings on both compilers (GCC -Wall -Wextra -Werror, MSVC /W4 /WX) so nonstandard extensions are caught early.
  • Don’t rely on compiler extensions for program logic; code that only compiles on one compiler is brittle.

’s suspicion that something else might be going on was reasonable, but in this case the root cause is the binding rule plus MSVC’s permissive behavior.

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.