Part 1ArticleWritten: execution sequence of constructor methods in Java,This is the execution sequence of the constructor in C ++.
Tested in the following order:
1. Execute the constructors of the static members first. If the static members are declared in the class definition but not implemented, they do not need to be constructed. The constructor must be executed after initialization.
2. Any abstract base class constructor is constructed in the inherited Order (not in the initialization List)
3. constructors of any virtual base classes are constructed in the inherited Order (not in the initialization List)
4. Any non-virtual base class constructors are constructed in the inherited Order (not in the initialization List)
5. constructors of any member objects are constructed in the order they are declared
6. Class self-built Functions
TestProgramAs follows:
# Include <iostream> # include <string> using namespace STD; // abstract class Aclass A {public: () {cout <"constructor of abstract class A" <Endl ;}// pure virtual function funvirtual void fun1 () = 0 ;}; // abstract class bclass B {public: B () {cout <"constructor of abstract class B" <Endl ;}// funvirtual void fun2 () = 0 ;}; // common class cClass c {public: C () {cout <"constructor of class C" <Endl ;}; // common class dclass d {public: D () {cout <"constructor of class D" <Endl ;}}; // common class cClass E {public: E () {cout <"constructor of Class E" <Endl ;}; // common class dclass f {public: F () {cout <"constructor of Class F" <Endl ;}; // common class dclass G {public: G () {cout <"constructor of Class G" <Endl ;}; // common class dclass H {public: H () {cout <"constructor of Class H" <Endl ;}; // common class dclass m {public: m () {cout <"constructor of class M" <Endl ;}; class test: Public A, public B, virtual public C, virtual public D, public e, public f {public: Test (): B (), A (), D (), C (), F (), E () {cout <"constructor of the test class" <Endl;} void fun1 () {} void fun2 () {} PRIVATE: g; static h; static M ;}; h test: H; int main (INT argc, char * argv []) {test; return exit_success ;}
Result:
Class H Constructor
Class C Constructor
Constructor of class D
Constructor of abstract class
Constructor of abstract class B
Class E Constructor
Constructor of Class F
Class G Constructor
Constructor of class test