I. Structure
The constructor cannot be a virtual function.
A derived class must first call the constructors of the base class, but cannot directly overwrite the constructors of the base class.
Therefore, You Cannot initialize the base class members in the derived class initialization list.
See the following example:
[Cpp]
01. class Base
02 .{
03. public:
04. Base (double dNum)
05.: nBase (1)
06., dBase (dNum)
07 .{
08.
09 .}
10. int ShowNum () {return nBase ;}
11. protected:
12. int nBase;
13. double dBase;
14. private:
15 .};
16.
17.
18. class Derived: public Base
19 .{
20.
21. public:
22. Derived (double dNum)
23.: Base (dNum)
24., nBase (3)
25 .{
26. nBase = 2;
27 .}
28. protected:
29. private:
30 .};
Class Base
{
Public:
Base (double dNum)
: NBase (1)
, DBase (dNum)
{
}
Int ShowNum () {return nBase ;}
Protected:
Int nBase;
Double dBase;
Private:
};
Class Derived: public Base
{
Public:
Derived (double dNum)
: Base (dNum)
, NBase (3)
{
NBase = 2;
}
Protected:
Private:
};
Error C2614: "Derived": Invalid member initialization: "nBase" is not a base or member at nBase (3 ).