Execution sequence of constructor in C ++

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.