I have read that the default assignment operator
is not generated by the compiler when the class contains a const field.
But I tried the scenario as in the below program and it works without any warning/error. If the above statement is not true,then can some one let me when is the default assignment operator not generated by the compiler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class base
{
constint a;
public:
base(int val = 10):a(val){}
int get()
{
return a;
}
};
int main()
{
base b;
int j = b.get();
cout<<j;
}
Correct - no default assignment operator will be generated.
You cannot assign anything to a class that contains const member variables.
Not true - you can write your own assignment operator and only assign the non-const members. It would be a pretty useless feature if making one member const rendered the whole class const.