Constructor initialization list and constructor body assignment

Source: Internet
Author: User

#include <iostream>using namespace  std;class a{public:a (int a,string str) {m_a = A;m_str = str;} A (int a,string str): M_a (a), m_str (str) {} void print () {cout << m_a << ' << m_str<< Endl;} Private:int m_a;string m_str;}; int main (int argc, char **argv) {A A (1, "Hello"); A.print (); return 0;}

Run the same results, then the difference between the two???

Conceptually, we can assume that the constructor is executed in two stages:

(1) Initialization phase (2) The normal calculation stage.

① a data member of a class type is always initialized in the initialization phase, regardless of whether the member is explicitly initialized in the constructor initialization list. Initialization occurs before the start of the calculation phase.

② does not display every member mentioned in the constructor initialization list, and initializes it with the same rules as the initialization variable. Run the default constructor for the type to initialize the data members of the class type.  

③ the initial value of a member of a built-in or composite type depends on the scope of the object: in the local scope, these members are not initialized, and in the global scope, they are initialized to 0.

The ④ calculation phase consists of all the statements in the constructor body. In the calculation phase, the setting of the data member is considered an assignment, not an initialization. There is no clear recognition that this distinction is a common source of procedural errors and inefficiencies.

⑤. Initialize! = Assignment.; Initialization represents allocating memory for a variable. A variable is initialized at its definition by the compiler (at compile time). In the function, the function parameter initialization occurs at the time of the function call (Runtime)., the assignment represents "erase object current value, give new value". It does not assume the obligation to allocate memory for an object.

Conclusion: The constructor initialization list initializes the members of the class, while the constructor body only assigns the data members of the class once.

1. The value of the initialization list exists:

first, the data members are categorized by type and described:
①. Built-in data types, composite types (pointers, references)
in the member initialization list and in the constructor body, both performance and results are the same
②. User-defined type (class type)
the results are the same, but there is a big difference in performance. The more popular implementation is to initialize the table with members: Because the data member object of the class type has been constructed before entering the function body, that is, the work of constructing the object at the member initialization list, calling the constructor, after entering the function body, is the assignment to the already constructed class object, A copy assignment operator is also called to complete (if not provided, the default per member assignment behavior provided by the compiler is used)

2. So when do you need a constructor initialization list?
(1) const member
(2) reference type member
(3)   in inheriting classes call the base class initialization constructor, which is essentially constructing the base class object and must use the initialization list.
Span style= "White-space:pre" >         above 3 scenarios you need to explicitly initialize a data member in the constructor initialization list. Because const and reference type members can only be initialized, they cannot be assigned a value operation.

In fact, we should try to use the initialization list instead of assigning a value in the constructor.

3. Sequence of initialization
The constructor initialization list simply specifies the initial value of the member and does not specify the initialization order, so what is the member initialization order? The order in which members are initialized is the order in which members are defined, the first defined member is initialized first, then the second one, and so on.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Constructor initialization list and constructor body assignment

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: 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.