20160403_c++ initialization List and assignment

Source: Internet
Author: User

Referenced from: http://www.cnblogs.com/BlueTzar/articles/1223169.html

1. Two types of initialization: constructor initialization list and constructor internal assignment (1) constructor Initialization list

The constructor initialization list begins with a colon, followed by a comma-delimited list of data members followed by an initialization in parentheses after each data member.

class cexample {public:    int  A;     float b;      Constructor initialization list    cexample (): A (0), B (8.8)    {
    A assigns a value of 0,b to 8.8
}};
(2) constructor Internal assignment
class cexample {public:    int  A;     float b;     // constructor Internal assignment     cexample ()    {        a=0;        b=8.8;    }};
(3) Comparison of two types of initialization methods:
    • initialization and assignment do not make much difference to members of built-in types, like any of the above constructors. for non-built-in type member variables, in order to avoid two constructs, recommended But sometimes you must use a constructor with an initialization list:
      1. Member types are classes that do not have a default constructor. If no display initialization is provided, the compiler implicitly uses the default constructor of the member type, and if the class does not have a default constructor, the compiler will fail with the default constructor.
      2. const member or member of reference type. Because const objects or reference types can only be initialized, they cannot be assigned a value.  
    • What is the meaning of initializing data members and assigning values to data members? What's the difference?
      First, the data members are categorized by type and described:
      1. Built-in data type, composite type (pointer, reference)
      In the member initialization list and in the constructor body, both performance and results are the same
      2. User-defined type (class type)
      The results are the same, but there is a big difference in performance. Because a 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, and the copy assignment operator is called to complete (if not provided, Use the default by member assignment behavior provided by the compiler)

2. Initialize the member initialization order of the list

When C + + initializes class members, it is initialized in the order in which they are declared , not in the order in which they appear in the initialization list.
Example:

class CMyClass {    cmyclass (intint  y);     int m_x;     int m_y;}; Cmyclass::cmyclass (intint  y): m_y (y), m_x (m_y) {}

You might think that the above code will do m_y=i first, then do m_x=m_y, and finally they have the same value.

But the compiler initializes the m_x first, then m_y, because they are declared in this order. The result is that the m_x will have an unpredictable value. There are two ways to avoid it, one is to always declare members in the order that you want them to be initialized, and the second is to always list those members in the order in which they are declared, if you decide to use the initialization lists. This will help eliminate confusion.

3. Examples

What is the output of the result variable, as in the following code?

#include <iostream>using namespacestd;intI=1;classmycls{ Public: Mycls (): M_nfor (M_NTHD), m_nsec (i+ +), M_nfir (i++), M_NTHD (i++) {M_NTHD=i; }    voidEcho () {cout<<"Result:"<<m_nFir+m_nSec+m_nThd+m_nFor<<Endl; }Private:    intM_nfir; intm_nsec; intM_NTHD; int&m_nfor;};intMain () {mycls ocls;    Ocls.echo (); return 0;}


Answer:

The first thing to understand is that the order in which the variables are initialized is the order of their declarations, regardless of the order of the initialization list. So the variables are initialized in the order of M_nfir (i++), M_nsec (i++), M_NTHD (i++), &m_nfor (M_NTHD);

I has an initial value of 1, so after initialization of the list initialization:
    • M_nfir (i++) uses I to assign a value of 1, then I to add 2
    • M_nsec (i++) uses I to assign a value of 2, then I self-add 3
    • M_NTHD (i++) uses I to assign a value of 3, then I self-add 4
    • M_nfir=1,m_nsec=2,m_nthd=3,m_nfor is a reference to the M_NTHD.

      And the value of I at this time is 4, after executing the statement m_nthd=i in the constructor, M_nthd=4,m_nfor is a reference to it, and the natural value is 4.

      Output result m_nfir+m_nsec+m_nthd+m_nfor=1+2+4+4=11

20160403_c++ initialization List and 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: 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.