I have the following simple C++ code:
#include <cstdio>
class A
{
public:
A(int y) : x(y) {}
A& operator=(const A& rhs);
int x;
};
A::A& A::operator=(const A& rhs) { this->x = rhs.x; return *this; }
int main(int, char**)
{
A a1(5);
A a2(4);
printf("a2.x == %d\n", a2.x);
a2 = a1;
printf("a2.x == %d\n", a2.x);
return 0;
}
Line 11, where the definition of A's operator=() function is at, is malformed...or, at least, I believe so. As expected, G++ 4.7.4, as well as every newer version of GCC that I've tried, throws the following error:
main.cpp:11:1: error: ‘A::A’ names the constructor, not the type
Oddly, though, G++ 4.4.7 compiles this program successfully, with no warnings, and even prints 4 and 5 as would be expected if line 11 were written correctly (i.e. with just A& instead of A::A&).
Can someone help me decipher what exactly is going on there with G++ 4.4.7? Is this just a bug in that release (albeit an extremely old one, and shame on us for still using it)? I'd think the standard would explicitly state how the operator=() function must be declared and defined.