[C++] Constructor calling rules (Default constructor | Default no-argument constructor | Default copy constructor | Constructor calling rule description)





1. Default constructor



There are two special constructors in C++ classes, namely:

  • Default parameterless constructor: If there is no constructor defined in the C++ class , the C++ compiler will automatically provide a "default parameterless constructor" for the class, and the function body will be empty and no operation will be performed;
  • Default copy constructor: If there is no copy constructor defined in the C++ class , the C++ compiler will automatically provide a "default copy constructor" for the class, and perform simple copy operations on member variables in the function;

1. Default parameterless constructor


If there is no constructor defined in a C++ class , the C++ compiler will automatically provide a "default no-argument constructor" for the class, and the function body will be empty and no operation will be performed;

  • No constructor defined: If there is no constructor defined for a C++ class, the C++ compiler will automatically generate a default no-argument constructor for the class;
  • Constructor defined: If other types of constructors (parameterized constructor/parameterless constructor/copy constructor) are defined for a C++ class, the C++ compiler will no longer automatically generate a default parameterless constructor;

Default constructor content: The default no-argument constructor defined by the C++ compiler for the class, its function body is empty, as shown in the following code;

class MyClass {
    
      
public:  
    int x;  
    // 默认构造函数  
    MyClass() {
    
    
    }  
};

The following code will automatically call the default no-argument constructor;

MyClass obj; // 调用 默认 构造函数

2. Default copy constructor


The "default copy constructor" is used to create a new object as a copy of an existing object. Its function is to copy the member variables of the existing object to the new object;

When a class object is created and assigned to another class object, the default copy constructor is automatically called;


If there is no copy constructor defined in the C++ class , the C++ compiler will automatically provide a "default copy constructor" for the class, and perform a simple copy operation on the member variables in the function;

  • No copy constructor defined: If there is no copy constructor defined for a C++ class, the C++ compiler will automatically generate a default copy constructor for the class;
  • A copy constructor is defined: If a copy constructor is defined for a C++ class, the C++ compiler will no longer automatically generate a default copy constructor;

Contents of the default copy constructor: The default copy constructor defined by the C++ compiler for a class assigns the member variables of the existing object to the new object one by one inside the function;

class MyClass {
    
      
public:  
    int x;  
    // 默认拷贝构造函数  
    MyClass(const MyClass& other) {
    
      
        x = other.x;  
    }  
};

In the following code, the first line of code will automatically call the default no-argument constructor, and the second line of code will automatically call the default copy constructor;

MyClass obj; 			// 调用默认无参构造函数
MyClass obj2 = obj; 	// 调用默认拷贝构造函数





2. Constructor calling rules




1. Description of constructor rules


Constructor calling rules:

  • Provide a default parameterless constructor and a default copy constructor: If there is no constructor defined in the C++ class , the C++ compiler will provide a default parameterless constructor and a default copy constructor;
  • Provide a default copy constructor: If a non-copy constructor is defined in a C++ class , such as: parameterized constructor/parameterless constructor, the C++ compiler will not provide a default parameterless constructor, but will provide a default copy constructor. function;
    • The default copy constructor can only copy member variables;
  • Special case: If a copy constructor is defined in a C++ class , the C++ compiler will not provide a default no-argument constructor;

2. Code example - only define the copy constructor


In the following code, the copy constructor is defined. The C++ compiler will not automatically generate the default no-argument constructor and the default copy constructor;

Use Student s;code to create objects and report errors “Student”: 没有合适的默认构造函数可用;

Explain that the C++ compiler does not generate a default no-argument constructor for this class;


Code example:

#include "iostream"
using namespace std;

class Student
{
    
    
public:
	Student(const Student& s)
	{
    
    
		m_age = s.m_age;
		m_height = s.m_height;
		cout << "调用拷贝构造函数" << endl;
	}

public:
	int m_age;		// 年龄
	int m_height;	// 身高
};


int main()
{
    
    
	// 定义了拷贝构造函数 不再生成默认无参构造函数
	Student s;


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
	return 0;
}

Results of the :

已启动生成…
1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1>hello_world.cpp
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(23,10): error C2512: “Student”: 没有合适的默认构造函数可用
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(4,7): message : 参见“Student”的声明
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0==========

Insert image description here


3. Code example - only define parameterized constructors


In the following code, a parameterized constructor is defined, and the C++ compiler will not automatically generate a default parameterless constructor and a default copy constructor;

Use Student s;code to create objects and report errors “Student”: 没有合适的默认构造函数可用;

Explain that the C++ compiler does not generate a default no-argument constructor for this class;


Code example:

#include "iostream"
using namespace std;

class Student
{
    
    
public:
	// 带参构造函数
	Student(int age, int height)
	{
    
    
		m_age = age;
		m_height = height;
		cout << "调用带参数构造函数 m_age = " << m_age << endl;
	}

	Student(const Student& s)
	{
    
    
		m_age = s.m_age;
		m_height = s.m_height;
		cout << "调用拷贝构造函数" << endl;
	}

public:
	int m_age;		// 年龄
	int m_height;	// 身高
};


int main()
{
    
    
	// 定义了拷贝构造函数 不再生成默认无参构造函数
	Student s;


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
	return 0;
}

Results of the :

已启动生成…
1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1>hello_world.cpp
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(31,10): error C2512: “Student”: 没有合适的默认构造函数可用
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(4,7): message : 参见“Student”的声明
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0==========

Insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/132896007
Recommended