I wouldn't have thought that your first example would give a error because the class has no data
members.
However suppose you had this:
1 2 3 4 5 6 7 8 9 10
|
class A
{
int x;
};
int main()
{
const A a;
return 0;
}
|
You notice that the class has a data member, it has no programmer defined constructor(s)
When you try to create the constant variable
const A a;, using the compiler suplied
default constructor, the compiler will not be happy -
because it would not know what values to give to the data member
x.
You would need to give
x a meaningful value at constrution time because you cannot change it afterwards.
Therefore, you need to supply your own constructor - the compiler is now happy because you have taken the responsibilty for constructing the objects.