C ++: data member initialization and assignment


1. default initialization

#include <iostream>
#include <string>
using namespace std;
class A
{
public:
    int m;
    string s;
};

int main()
{
    A a1;
    cout << "----------------------------" << endl;
    cout << a1.m << endl;//类内内置类型未定义
    cout << a1.s << endl;//string默认初始化为空串
    cout << "----------------------------" << endl;
    return 0;
}

operation result

  1. When the class no class constructor and the initial value , the compiler is initialized to the default data members by a synthetic default constructor.
  2. Default initialized, variables are assigned a default value determined by the type and location of the variable within the body of the function built-in types and classes are not initialized, i.e., undefined.

2. The initial value of the class (compiler does not support portion)

#include <iostream>
#include <string>
using namespace std;
class A
{
public:
    int m = 1;
    string s = "a";
};

int main()
{
    A a1;
    cout << "----------------------------" << endl;
    cout << a1.m << endl;
    cout << a1.s << endl;
    cout << "----------------------------" << endl;
    return 0;
}

operation result

  1. When the class no constructor, but there are classes initial value , the compiler synthesis default constructor, by using the initial value to initialize class data member.

3. assignment within the constructor

#include <iostream>
#include <string>
using namespace std;
class A
{
public:
    A() //显式创建的构造函数
    {
        m = 2;
        s = "b";
    }
    int m = 1;
    string s = "a";
};

int main()
{
    A a1;
    cout << "----------------------------" << endl;
    cout << a1.m << endl;
    cout << a1.s << endl;
    cout << "----------------------------" << endl;
    return 0;
}

operation result

  1. Created explicitly within a class constructor, the function of an assignment, the results will cover the class initializer.
  2. If the members are const, reference types, or some of this class is not provided a default constructor , its members must be initialized, the method may not be assigned.

4. constructor initialization list

#include <iostream>
#include <string>
using namespace std;
class B //不含默认构造函数
{
public:
    int n;
    B(int x) //默认构造函数无参数,此构造函数不是默认构造函数
    {
        n = x;
    }
};

class A
{
public:
    A() : m1(10), m2(20), r(m2), b(40) //显示创建的构造函数
    {
        m1 = 100;
    }
    int m1 = 1;
    const int m2 = 2;  //const
    const int &r = m1; //引用
    B b;               //未提供默认构造函数
};

int main()
{
    A a1;
    cout << "----------------------------" << endl;
    cout << a1.m1 << endl;
    cout << a1.m2 << endl;
    cout << a1.r << endl;
    cout << a1.b.n << endl;
    cout << "----------------------------" << endl;
    return 0;
}

operation result

  1. If the members are const, reference types, or some of this class is not provided a default constructor , its members must initialization list constructor initializes.
  2. Priority: the default initialization <class initializer <constructor initialization list <constructor assignment. Value data member is a high priority value coverage.

note:

#include <iostream>
#include <string>
using namespace std;
class B //不含默认构造函数
{
public:
    int n;
    B(int x, int y) //默认构造函数无参数,此构造函数不是默认构造函数
    {
        n = x * y;
    }
};

class A //类内初始值
{
public:
    int m1 = 1;
    const int m2 = 2; //const
    int &r = m1;      //引用
    B b = B(3, 4);    //未提供默认构造函数
};

int main()
{
    A a1;
    cout << "----------------------------" << endl;
    cout << a1.m1 << " " << a1.m2 << " " << a1.r << " " << a1.b.n << endl;
    a1.m1 = 0; //m1是int,可修改
    // a1.m2 = 0; //错误,m2是const int,不可修改
    a1.r = 5;       //r引用m1,对r的修改实际是对m1的修改
    a1.b = B(0, 0); //b是类类型,可赋值修改
    cout << a1.m1 << " " << a1.m2 << " " << a1.r << " " << a1.b.n << endl;
    cout << "----------------------------" << endl;

    A a2, a3; //a2.m2和a3.m2不可修改,且总是相等
    cout << a2.m2 << endl;
    cout << a3.m2 << endl;
    cout << "----------------------------" << endl;
    return 0;
}

operation result

#include <iostream>
#include <string>
using namespace std;
class B //不含默认构造函数
{
public:
    int n;
    B(int x, int y) //默认构造函数无参数,此构造函数不是默认构造函数
    {
        n = x * y;
    }
};

class A //构造函数初始化列表
{
public:
    A() : m1(1), m2(2), r(m1), b(3, 4) {}                               //默认构造函数
    A(int i1, int i2, int x, int y) : m1(i1), m2(i2), r(m1), b(x, y) {} //普通构造函数

    int m1;
    const int m2; //const
    int &r;       //引用
    B b;          //未提供默认构造函数
};

int main()
{
    A a1;
    cout << "----------------------------" << endl;
    cout << a1.m1 << " " << a1.m2 << " " << a1.r << " " << a1.b.n << endl;
    a1.m1 = 0; //m1是int,可修改
    // a1.m2 = 0; //错误,m2是const int,不可修改
    a1.r = 5;       //r引用m1,对r的修改实际是对m1的修改
    a1.b = B(0, 0); //b是类类型,可赋值修改
    cout << a1.m1 << " " << a1.m2 << " " << a1.r << " " << a1.b.n << endl;
    cout << "----------------------------" << endl;

    A a2(1, 2, 3, 4), a3(10, 20, 30, 40); //a2.m2和a3.m2不可修改,但a2.m2和a3.m2可以不同
    cout << a2.m2 << endl;
    cout << a3.m2 << endl;
    cout << "----------------------------" << endl;
    return 0;
}

operation result

  1. When a member is const, or some reference does not provide a default constructor for this class type of time, with the initial value of the class initialization, the compiler is not given, the program running.
  2. When initializing const class members in the initial value used when the object class can not modify their const member, different objects which const member are equal , no difference const const outside the class in the class.
  3. When using a constructor initialization list to initialize a const member , the object class can not modify your const member, but a different object whose value const member may be different , in line with usage class.
Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/104873403