Default assignment operator

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
{
	const int a;
public:
	base(int val = 10):a(val){}
	
	int get()
	{
		return a;
	}
};
int main()
{
	base b;
	int j = b.get();
	cout<<j;
}
It won't be generated for you if you declare it yourself.
Can you elaborate please !!!
The below link from MSDN says this gives up a arning message :
http://msdn.microsoft.com/en-us/library/hsyx7kbz(v=vs.80).aspx
I have read that the default assignment operator
is not generated by the compiler when the class contains a const field.
Yes it's true. You cannot assign anything to a class that contains const member variables.

The default assignment operator is this:
base &operator=(const base &b);
So are there any other conditions where the default assignment operator is not generated?
When a class has one or more members that are a reference.
Yes it's true.

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.

Cheers,
Jim
Topic archived. No new replies allowed.