#include <iostream.h>class a{ constint A; int b;}; void Main () { A obja;}
The following error occurred in the compilation: Error C2512: ' A ': No appropriate default constructor available;
If you remove the const, you're right!
#include <iostream.h>classa{ Public: Const intA; intb; A (intx): A (x) {}};voidMain () {A Obja (2); cout<<obja.a<<Endl; cout<<obja.b<<endl;//is not automatically initialized}
There are two ways to initialize a constructor
1 the way in which an assignment statement is used in the body of a constructor.
2 The way the list is initialized with a constructor function.
It is important to note that the execution of the initialization list precedes the execution of the constructor body. Also, some special members, such as constant members and reference members, must be initialized in the initialization list.
This means that once you have a const member or reference member in a class, you cannot use the default constructor. It must be initialized with a user-defined constructor, and it is initialized in the same way that an expression is used.
#include <iostream.h>class a{ public: int &A; int b;
//a is a reference to B A (): B (9), a (b) {}//references and const members must be used in this form. }; void Main () { A obja; cout<<obja.a<<Endl;}
#include <iostream.h>classa{ Public: Const intA; A (intx): A (x) {}};classB: Publica{ Public: B (intx): A (x) {}};voidMain () {A Obja (2); B OBJB (3); cout<<obja.a<<Endl; cout<<objb.a<<Endl;}
How a const member or reference member must initialize a list by using a constructor