Consider the following code:
#include <iostream> class MyClass { const int value; public: explicit MyClass() : value(10) { } MyClass(int val) : value(val) { } int GetValue() { return value; } }; int main() { MyClass a, b(20); std::cout << "a.value = "<< a.GetValue() << " b.value = "<< b.GetValue() << std::endl; b = a; std::cout << "a.value = "<< a.GetValue() << " b.value = "<< b.GetValue() << std::endl; return 0; }
When built with Composer 2013 SP1 Update 4 on Windows, I get:
a.value = 10 b.value = 20
a.value = 10 b.value = 10
Two questions:
1. Why does this compile? Neither g++ nor MSVC will compile it because of the const member, which cannot be overwritten in the default assignment operator.
2. Why does assignment modify the const value?