Constructor initializer list, the default constructor and a constructor with embedded objects of knowledge

Background: In the process of learning to use C ++ to work, the number of pit encountered some aspects of the initialization, data queries a little, then too this article

1. constructor initializes

In the constructor initializer list initialization with data fields

Class Person (parameterList): dataField1value1), dataField2value2

{

  // Something to do

}

2. Why do you need constructor initializer list

BACKGROUND: class data field is an object type, the object is called object or embedded object, the embedded objects must be configured to complete before being embedded object constructor body executed so only the front body of the function was added at a location other norms colon, achieved constructor initializes

class Time { .... }

class Action 
{
private:

    int index;
    Time time;

public:
    //time对象应该在构造函数体之前构造完成
    Action(int index, int hour, int minute, int second):index(index),time(hour, minute, second) {}
};

3. The default constructor

The default constructor is a constructor with no arguments can be invoked either be defined as a constructor parameter list is empty, it can be all the parameters have a default constructor parameter values

class Circle1 
{
public:
    Circle1() // 无参数
    {      
        radius = 1.0; /*函数体可为空*/
    }
private:
  double radius;
};


class Circle2 
{
public:
    Circle2(double r = 1.0): radius(r) {} // 所有参数都有默认值
private:
    double radius;
};

Note bug point: If a class has a non-default constructor, then the compiler will not generate a default constructor for the class (and sometimes default constructor must also have)

3.1. If the object type member / members of the embedded object not explicitly initialized

Constructor explicit initialization means provided in the class declaration constructor prototype, and provides a implementation code; and implicit initialization means when you do not provide a constructor function, C ++ automatically provides a constructor , and initialization .

(1) of the embedded object no-argument constructor is called automatically

(2)  If there is no embedded object at this time no argument constructor, the compiler error

class Cirle
{
.......
}
class X
{
private:
    Circle c1;
public:
    X(){}
}
//此时c1中无默认构造函数就会出错(被顶了)

3.2. You can also manually construct objects in the initialization list

 

class Cirle
{
.......
};
class X
{
private:
    Circle c1;
public:
    X():c1(...){}
};

3.3. If the data field is a type of object class (and it does no argument constructor), the object may be initialized into the constructor initializer list

Here I want to say is this above that case, but why can not be, because there are in-place initialization (seemingly) in C ++ 11

to sum up:

C ++ initialization way reportedly as many as eighteen kinds, wrote when you can pinpoint one kind

Guess you like

Origin blog.csdn.net/shanshenyuyou/article/details/94571603
Recommended