Hi all, is there a good way to define a assignment constructor for const members? Below code doesnot compile for function Foo& operator=(const Foo &f),
Initialization lists cannot be used on anything other than constructors and arrays (the version for arrays is different syntactically). Once the constant member is assigned a value, it's value remains constant.
A const member simply cannot be assigned to, and non-static const member makes the entire class non-assignable*
1 2 3 4 5 6 7
constint ans = 42;
constint n = 77;
ans = n; // does not compile
Foo f1(42); // Foo holds a const int m_val inside
Foo f2(77);
f1 = f2; // why do you expect this to compile?
* technically, you could write a copy-assignment operator that doesn't attempt to modify m_val and that will compile, but after such a = b; a and b will not be equal, and that's rarely acceptable.