C++ Object-Oriented (Part 2)

 The general content of the explanation is initialization list, explicit keyword, static (static) members


Table of contents

Initialization list and function constructor assignment

function constructor assignment

initialization list

Initialization order using initializer list

Advantages of Using Initialization Lists

explicit keyword

static member (static)

concept


Initialization list and function constructor assignment

function constructor assignment

When creating an object, the compiler will give each member variable in the object an appropriate initial value by calling the constructor:

class A
{
    public:
    // 构造函数
    Date(int X = 0, int Y = 0, int Z = 0)
    {
        _X = X;
        _Y = Y;
        _Z = Z;
    }
private:
    int _X;
    int _Y;
    int _Z;
};

Note: The above function constructor assignment can be assigned multiple times, and the initialization list we will talk about below

initialization list

1. Each member variable can only appear once in the initialization list  because initialization can only be performed once , so the same member variable cannot appear multiple times in the initialization list .

class A
{
    public:
    // 构造函数
    Date(int X = 0, int Y = 0, int Z = 0)
        :_X(X)
        ,_Y(Y)
        ,_Z(Z)
    {}
private:
    int _X;
    int _Y;
    int _Z;
};

2. The class contains the following members, which must be placed in the initialization list for initialization:

1. Reference member variables  Variables of reference type must be given an initial value when they are defined, so reference member variables must be initialized using an initialization list.

class A
{
    public:
    // 构造函数
    Date(int X = 0, int Y = 0, int Z = 0)
        :_X(X)
        ,_Y(Y)
        ,_Z(Z)
    {}
private:
​
    int& _b = 0;// 创建时就初始化
};

2. The const member variable  modified by const must also be given an initial value when it is defined, and must also be initialized using the initialization list.

class A
{
    public:
    // 构造函数
    Date(int X = 0, int Y = 0, int Z = 0)
        :_X(X)
        ,_Y(Y)
        ,_Z(Z)
    {}
private:
    
    const int a = 10;//correct 创建时就初始化
};
 
 

3. Custom type members (this class has no default constructor)  If a class does not have a default constructor , then we need to pass parameters to initialize it when instantiating the class object , so instantiate a class object without a default constructor must be initialized using an initializer list .

class B
{
    public:
    // 没有默认构造
    Date(int Big)
      :_Big(Big);
    {}
private:
    int _Big;
};
​
class A
{
    public:
    // 构造函数
    Date(int A = 0)
        :_Big(A)
    {}
private:
    int _A;
    B _Big;
};

 Here again, the default constructor refers to a constructor that can be called without passing parameters:

 1. We don't write, the constructor is automatically generated by the compiler. 2. No parameter constructor. 3. All default constructors.

Initialization order using initializer list

for example:

class A
{
    public:
    // 构造函数
    Date(int X = 0, int Y = 0, int Z = 0)
        :_X(X)
        ,_Y(Y)
        ,_Z(Z)
    {}
private:
    //初始化列表的初始化顺序
    int _X;//1
    int _Y;//2
    int _Z;//3
};

Note: The initialization order of the initialization list is initialized from top to bottom according to your definition in private

Advantages of Using Initialization Lists

 Because the initialization list is actually the place where the member variables of the object are defined when you instantiate an object, so whether you use the initialization list or not, you will go through such a process (the member variables need to be defined). Strictly speaking: 1. For built-in types, there is actually no difference between using the initialization list and initializing in the constructor body. The difference is similar to the following

1. For built-in types, there is practically no difference between using the initialization list and initializing in the constructor body

2. For custom types, using the initialization list can improve the efficiency of the code

explicit keyword

After using this keyword explicit to modify the constructor , the implicit conversion of the single-argument constructor cannot be used .

class A
{
public:
    // 构造函数
    Date(int X = 0)
    _X = X;
    {}
    Print()
    {
        cout<< _X <<endl;
    }
private:
    int _X;
};
int main()
{
    A a1 = 100;
    return 0;
}
 
 

So in the early compilers, when the compiler encounters the code A a1 = 100, it will first construct a temporary object, and then use the temporary object to copy and construct a1; but the current compiler has been optimized, when it encounters When the code A a1 = 100, this is called implicit type conversion .

static member (static)

concept

 Class members declared static are called static members of the class. Member variables modified with static are called static member variables; member functions modified with static are called static member functions. Static member variables must be initialized outside the class , it is in the static area (in the memory model).

Static members are shared by all members of the class and do not belong to a specific class.

class A
{
private:
    static int _a;
};
int main()
{
    cout << sizeof(A) << endl;
    return 0;
}

It outputs a size of 1 because it is a static member _a (in the static area), which belongs to the entire class . So when calculating the size of a class or the size of a class object, static members are not counted.

Static member variables must be defined and initialized outside the class

class A
{
private:
    static int _a;
};
int main()
{
    cout << sizeof(A) << endl;
    return 0;
}
int A::_A = 0;//定义初始化

Ways to access static member variables

class A
{
public:
    static int _a; //静态,属于整个类
};
// 静态成员变量的定义初始化
int A::_a = 0;
int main()
{
    Test test;
    cout << A._a << endl; //1.通过类对象突破类域进行访问
    cout << A()._a << endl; //3.通过匿名对象突破类域进行访问
    cout << A::_a << endl; //2.通过类名突破类域进行访问
    return 0;
}
  • The first one is: access through the class object to break through the class domain

  • The second is: access through anonymous objects to break through the class domain

  • The third is: access through the class name to break through the class domain

Static member functions have no this pointer

class A
{
public:
    static void Fun()
    {
        cout << _a << endl; //不能访问非静态成员
        cout << _b << endl; //可以
    }
private:
    int _a; //非静态成员
    static int _b; //静态成员
}

Note: Static members, like ordinary members of a class, also have three access levels: public, private and protected 

Note:

1. Can a static member function call a non-static member function?

  • Can't. Because the first formal parameter of a non-static member function defaults to the this pointer, and there is no this pointer in a static member function, a static member function cannot call a non-static member function.

2. Can a non-static member function call a static member function?

  • Can. Because both static member functions and non-static member functions are in the class, they are not restricted by the access qualifier in the class.

Guess you like

Origin blog.csdn.net/qq_45591898/article/details/130755722