C/C++ programming: default constructor

Default constructor

Default constructor: it is a constructor called without parameters , including two types:

  • No parameters
  • Each parameter has an initial value

The default constructor is called during default initialization and value initialization .

  • Default initialization: initialization performed when the variable is constructed without using the initializer.
  • Value initialization: initialization performed when the object is constructed with an empty initializer

Origin

Look at the following code first. The code
Insert picture description here
content is very simple. It defines a class Point containing members x and y. This class can be used in the place of the sequence.
Insert picture description here
Although we have not defined the constructor of the Point class, we can still define the pt object of the Point class and use it. The reason is that the compiler will automatically generate a default constructor, which has the same effect. at
Insert picture description here
However, once the parameters have to add other constructors, the compiler will not generate a default constructor
Insert picture description here

C++11 solution

C++11 allows us to use =defaultto require the compiler to generate a default constructor: in
Insert picture description here
this way, we can continue our good life.

#include <stdio.h>

struct Point {
    
    
    Point() = default;
    Point(int _x, int _y) : x(_x), y(_y){
    
    }
    int x;
    int y;
};


int main() {
    
    
    Point point;
    printf("%d, %d", point.x, point.y);
}

Insert picture description here

If it is a non-argument constructor written by yourself, you need to specify how the members are constructed. The default constructor will initialize the data members by default, and no additional specification is required. This saves some trouble.

Since integer is a built-in type, and the default initialization of integer members is not initialized, the x and y in this example still need to be initialized in the class. This is another topic.

grammar

grammar:

grammar Description The
类名 ( ) ; (1)
类名 :: 类名 ( ) 函数体 (2)
类名() = delete ; (3) Since C++11
类名() = default ; (4) Since C++11
类名 :: 类名 ( ) = default ; (5) Since C++11

Which 类名must name the current class (or the current instantiation of the class template), or must be a qualified class name when declared in the namespace scope or friend declaration.

  1. Default constructor declaration in class definition
struct Point {
    
    
    Point(){
    
    
        x = 0;
        y = 0;
    };
    int x;
    int y;
};
  1. The definition of the default constructor outside the class definition (the class must contain a declaration 1)
struct Point {
    
    
    Point();  //声明1
    int x;
    int y;
};
Point::Point(){
    
    
    x = 10;
    y = 0;
};

Insert picture description here

  1. Discarded default constructor: If it is 重载决议selected, the program fails to compile.
struct Point {
    
    
    Point() = delete ;  // 错误:使用了被删除的函数‘Point::Point()’
    int x;
    int y;
};
  1. Preset default constructor: Even if other constructors exist, in some cases the compiler will define the implicit default constructor.
#include <stdio.h>

struct Point {
    
    
    Point() = default;
    Point(int _x, int _y) : x(_x), y(_y){
    
    }
    int x;
    int y;
};


int main() {
    
    
    Point point;
    printf("%d, %d", point.x, point.y);
}

Insert picture description here
5. The preset default constructor outside the class definition (the class must contain a declaration (1)). This constructor is considered user-provided (see below and value initialization ).

struct Point {
    
    
    Point() ;  // 错误:使用了被删除的函数‘Point::Point()’
    int x;
    int y;
};

Point::Point() = default;

Implicitly declared default constructor

  • If no user-declared constructor is provided for a class type (struct, class, or union), the compiler will always declare a default constructor as an inline public member of its class.
#include <stdio.h>

struct Point1 {
    
    
    int x;
    int y;
};

class Point2 {
    
    
public:
    int x;
    int y;
};
union Point3 {
    
    
    int x;
    int y;
};

int main() {
    
    
    Point1 point1;
    Point2 point2;
    Point3 point3;
    printf("%d, %d\n", point1.x, point1.y);
    printf("%d, %d\n", point2.x, point2.y);
    printf("%d, %d\n", point3.x, point3.y);
}

Insert picture description here

  • Since C++11, when there is a user-declared constructor, the user can still use the keyword default to force the compiler to automatically generate the implicitly-declared default constructor.
#include <stdio.h>

struct Point1 {
    
    
    Point1() = default;  // 必须,当用户最定义了有参构造函数时编译器不会自己生成默认构造函数
    Point1(int _x, int _y) : x(_x), y(_y){
    
    }
    int x;
    int y;
};

class Point2 {
    
    
public:
    Point2() = default; // 必须,当用户最定义了有参构造函数时编译器不会自己生成默认构造函数
    Point2(int _x, int _y) : x(_x), y(_y){
    
    }
    int x;
    int y;
};
union Point3 {
    
    
    Point3() = default; // 必须,当用户最定义了有参构造函数时编译器不会自己生成默认构造函数
    Point3(int _x) : x(_x){
    
    }
    int x;
    int y;
};

int main() {
    
    
    Point1 point1;
    Point2 point2;
    Point3 point3;
    printf("%d, %d\n", point1.x, point1.y);
    printf("%d, %d\n", point2.x, point2.y);
    printf("%d, %d\n", point3.x, point3.y);
}

Insert picture description here

Instance

class A
{
    
    
public:
	A();  //没有参数
};
class B
{
    
    
public:
	explicit B(int x = 1, bool b = true);  //每个参数有初始值
	//explicit:阻止执行隐式转换,但是可以显示类型转换
};
class C
{
    
    
public:
	explicit C(int c);  //非默认构造函数
};

struct A
{
    
    
    int x;
    A(int x = 1): x(x) {
    
    } // 用户定义默认构造函数
};
 
struct B: A
{
    
    
    // 隐式定义 B::B(),调用 A::A()
};
 
struct C
{
    
    
    A a;
    // 隐式定义 C::C(),调用 A::A()
};
 
struct D: A
{
    
    
    D(int y): A(y) {
    
    }
    // 不会声明 D::D(),因为存在另一构造函数
};
 
struct E: A
{
    
    
    E(int y): A(y) {
    
    }
    E() = default; // 显式预置,调用 A::A()
};
 
struct F
{
    
    
    int& ref; // 引用成员
    const int c; // const 成员
    // F::F() 被隐式定义为弃置
};
 
int main()
{
    
    
    A a;
    B b;
    C c;
//  D d; // 编译错误
    E e;
//  F f; // 编译错误
}

https://author.baidu.com/home?from=bjh_article&app_id=1597683592389912

Guess you like

Origin blog.csdn.net/zhizhengguan/article/details/114990126
Recommended