[C++] Constructor meaning (explicit and implicit constructor calls | constructor alternatives - initialization function | initialization function defects | default constructor)





1. The meaning of constructor function




1. Class constructor


The constructors and destructors provided by C++ serve as initialization and destruction solutions for class instance objects;

Define several instance objects of a class, each object has independent member attributes;

When defining an instance object of a class, the C++ compiler will automatically call the developer-defined constructor;


2. Explicit calling and implicit calling of constructor


The constructor of a class can be called automatically or manually;

  • Generally, the default no-argument constructor is automatically called implicitly;
  • The parameterized constructor is explicitly called manually by the developer;

3. Alternative to constructor - initialization function


Alternative to constructor - initializer function:

  • Shared initialization function: Define a public shared initialization function for each class;
  • Calling timing: Initialization operation needs to be performed immediately after creating the object;

4. Initialization function defects


Disadvantages of initialization function and constructor:

  • The operation is cumbersome: the initialization function is just a common public function, which must be called manually by the developer, and it is called explicitly, which makes the operation cumbersome;
  • Omissions of operations: Use the initialization function to initialize the instance object, and there must be no omissions. If the instance object is not initialized, the member variable values ​​​​are random values, which are uncertain and cause unknown risks;
  • Unable to call: In some special occasions, the initialization function cannot be called. For example, if only the variable type of the class is defined and the constructor is not called, the parameterless constructor will be automatically called to initialize the instance object. If the initialization function is used, Then the initialization operation cannot be performed;

5. Default constructor


If a constructor is not written when defining a class, the C++ compiler will provide a default constructor for the class;

The function body of the default constructor is empty and does not need to be defined by developers. It is provided by the C++ compiler by default;


Default constructors are divided into 2 types:

  • Default parameterless constructor: If there is no constructor defined in the class, the C++ compiler will provide a parameterless constructor by default, and the function body of the constructor is empty;
  • Default copy constructor: If there is no copy constructor defined in the class, the C++ compiler will define a default copy constructor by default, which is used for simple member variable assignment;

6. Code example - the initialization function cannot be called in time


In the following code, in the Student class, the initialization function is defined:

	// 初始化函数 初始化 实例对象
	void init(int _age, int _height)
	{
    
    
		m_age = _age;
		m_height = _height;
	}

In scenarios where code is used Student sto define instance objects, the initialization function cannot be called in time;


Code example - initialization function:

#include "iostream"
using namespace std;

class Student
{
    
    
public:
	// 初始化函数 初始化 实例对象
	void init(int _age, int _height)
	{
    
    
		m_age = _age;
		m_height = _height;
	}

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

int main()
{
    
    
	// 定义 Student 类实例对象
	// Student 类 没有提供构造函数 , C++ 编译器 会提供一个 默认构造函数
	// Student 类 没有提供拷贝构造函数 , C++ 编译器 会提供一个 默认拷贝构造函数
	Student s;
	// 调用初始化函数
	s.init(18, 170);


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

Results of the :

Insert image description here

Guess you like

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