Please support our C++ advertiser: Programming Forums
Views: 969 | Replies: 2
![]() |
•
•
Join Date: Jun 2006
Location: Bangalore, India
Posts: 88
Reputation:
Rep Power: 3
Solved Threads: 3
#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 ?????
•
•
Join Date: Jun 2006
Location: Bangalore, India
Posts: 88
Reputation:
Rep Power: 3
Solved Threads: 3
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 102
I don't think there is a reason for GCC to give an error because of the missing
It ought to be something else. But I dont know what. Maybe GCC is guarding against self-assignment (which is possible if
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. バルサミコ酢やっぱいらへんで
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode