C # object initialization sequence and C ++ object initialization sequence

Source: Internet
Author: User

C #Object initialization

1.Variable first and then constructor. The variable is initialized first.,Then the constructor is executed.

2.First static and then instantiated. When a class is accessed,Static variables and constructors are first initialized.Then, the object's instantiated variables and constructors are initialized.

3. First, the derived class and then the base class. For variables and static Constructors , The derived object is initialized before the base object. . For example C Class derived from B Class , B Class derived from A Class , The initialization order of variables and static constructors is C-B-A.

4. besides the instance constructor. For instance constructors , the base class constructor is executed before the derived class constructor , the instance constructor is executed in the order of A-B-C.

5. do not assume the order of variables. fields initialize according to the declared sequence in the source file . however, , since the Program staff and tools can schedule variable declarations at will, , You should not initialize variables in any special order

6.The virtual method is constructed in two phases. Avoid calling Virtual Methods From a constructor.If you need to call some virtual methods when initializing an object,The two-phase building should be used in the complete construction of the object, and then the initialization method of the constructed object should be called.

 

C ++Constructor Call Sequence

1.If the class contains a member class, the constructor of the member class is called first;

2.Create the object of the derived class. The base class constructor is called first (also takes precedence over the member classes in the derived class );

3.If a base class constructor has multiple base classes, the calling sequence of the constructor is the sequence in which a class appears in the class derived table, rather than the sequence in the member initialization table;

4.If a member class object constructor has multiple Member class objects, the calling sequence of the constructor is the order in which objects are declared in the class rather than the order in the member initialization table;

5. constructor of the derived class, as a general rule, the derived class constructor should not directly assign values to a base class data member, but pass the values to the appropriate base class constructor. , otherwise, the implementation of the two classes will become tightly coupled ( tightly coupled ) it is more difficult to modify or extend the implementation of the base class correctly. (The responsibility of the base class designer is to provide a set of appropriate base class constructor)

Related Article

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.