[C++] Constructor Classification ③ (Method of calling a parameterized constructor | Parentheses method | Equal sign method)





1. Create instance objects of classes in different memories



In the previous blog [C++] Constructor Classification ② (Creating instance objects of a class in different memories | Creating instance objects in stack memory | Creating objects with the new keyword), the creation of objects in stack memory and heap memory was analyzed Two situations;

In this blog, we will continue to analyze the two methods of calling a parameterized constructor in the stack memory:

  • bracketing
  • equal sign method

C++ class member variables are:

public:
	int m_age;
	char* m_name;

Afterwards, this member variable is used as a reference to assign values ​​to these two member variables;


1. Calling the constructor using bracket method


First, in the Student class, define two constructors with parameters, and then use the bracket method to call the above constructors;

	// 有参构造函数
	Student(int age, const char* name)
	{
    
    
		// 方法体 省略
		cout << "调用有参构造函数" << endl;
	}

Not recommended usage: Manually call the parameterized constructor through the Student(18, "Tom") method. The above code will generate an anonymous Student instance object, and then assign the anonymous object to the Student s2 variable in the stack memory. ;

After the anonymous object is created, it will be initialized immediately;

	// 手动调用有参构造函数
	// 直接调用构造函数会产生匿名对象, 涉及到该匿名对象的生命周期
	Student s2 = Student(18, "Tom");

Recommended usage: To create an instance object of a class in stack memory, it is recommended to use the following method. Use parentheses directly after the declared stack memory variable name, and pass in the parameters of the constructor;

	// 使用 括号法 调用有参构造函数
	Student s4(1, "J");

Calling Student s4(1, "J") to create an instance object is equivalent to calling the Student(int age, const char* name) parameterized constructor, and then assigning the instance object to the s4 variable;


2. Call the constructor using the equal sign method


First, define a single-parameter constructor;

	// 有参构造函数
	Student(const char* name)
	{
    
    
		// 为 m_name 成员分配内存
		m_name = (char*)malloc(100);

		// 为 m_name 成员设置字符串值
		if (m_name != NULL)
		{
    
    
			strcpy_s(m_name, sizeof(name), name);
		}

		// 为 m_age 成员设置初始值
		m_age = 0;

		cout << "调用有参构造函数" << endl;
	}

Use the equal sign method to call a parameterized constructor with one parameter,

Assigning the string directly to the Student s5 variable is equivalent to calling the Student(const char* name) parameterized constructor and assigning the created instance object to the s5 variable.

This is a C++ =enhancement to the equal sign operator;

	// 使用 等号法 调用 有一个参数的 有参构造函数
	// C++ 对等号进行了功能增强
	Student s5 = "K";




2. Complete code example



In the code below,

When declaring the Student class, we define:

  • Default parameterless constructor
  • Parametric constructor - 1 parameter
  • Parametric constructor - 2 parameters
  • Copy constructor - a parameterized constructor with 1 parameter, the function type is Student(const Student& obj)

When calling the constructor to create an instance object, use

  • Directly declare the Student variable and automatically call the default no-argument constructor.
  • Use parentheses to call a 2-parameter parameterized constructor
  • Call copy constructor
  • Use the equal sign method to call the 1-parameter parameterized constructor

Code example:

#include "iostream"
using namespace std;

class Student
{
    
    
public:

	// 无参构造函数
	Student()
	{
    
    
		// 为 m_name 成员分配内存
		m_name = (char*)malloc(100);

		// 为 m_name 成员设置字符串值
		if (m_name != NULL)
		{
    
    
			strcpy_s(m_name, 5, "name");
		}

		// 为 m_age 成员设置初始值
		m_age = 0;

		cout << "调用无参构造函数" << endl;
	}

	// 有参构造函数
	Student(const char* name)
	{
    
    
		// 为 m_name 成员分配内存
		m_name = (char*)malloc(100);

		// 为 m_name 成员设置字符串值
		if (m_name != NULL)
		{
    
    
			strcpy_s(m_name, sizeof(name), name);
		}

		// 为 m_age 成员设置初始值
		m_age = 0;

		cout << "调用有参构造函数" << endl;
	}

	// 有参构造函数
	Student(int age, const char* name)
	{
    
    
		// 为 m_name 成员分配内存
		m_name = (char*)malloc(100);

		// 为 m_name 成员设置字符串值
		if (m_name != NULL)
		{
    
    
			strcpy_s(m_name, sizeof(name), name);
		}

		// 为 m_age 成员设置初始值
		m_age = age;

		cout << "调用有参构造函数" << endl;
	}

	// 拷贝构造函数
	Student(const Student& obj)
	{
    
    
		// 为 m_name 成员分配内存
		m_name = (char*)malloc(100);

		// 为 m_name 成员设置字符串值
		if (m_name != NULL)
		{
    
    
			strcpy_s(m_name, sizeof(obj.m_name), obj.m_name);
		}

		// 为 m_age 成员设置初始值
		m_age = obj.m_age;

		cout << "调用拷贝构造函数" << endl;
	}

	~Student()
	{
    
    
		// 销毁 name 指向的堆内存空间
		if (m_name != NULL)
		{
    
    
			free(m_name);
		}
		cout << "调用析构函数" << endl;
	}

public:
	int m_age;
	char* m_name;
};

int main()
{
    
    
	// 声明 Student 类型实例对象
	// 调用无参构造函数
	Student s1;
	// 打印 Student s1 实例对象值
	cout << "name : " << s1.m_name << " , age : " << s1.m_age << endl;

	// 手动调用有参构造函数
	// 直接调用构造函数会产生匿名对象, 涉及到该匿名对象的生命周期
	Student s2 = Student(18, "Tom");
	// 打印 Student s1 实例对象值
	cout << "name : " << s2.m_name << " , age : " << s2.m_age << endl;

	// 使用 括号法 调用有参构造函数
	Student s4(1, "J");
	// 打印 Student s4 实例对象值
	cout << "name : " << s4.m_name << " , age : " << s4.m_age << endl;

	// 使用 等号法 调用 有一个参数的 有参构造函数
	// C++ 对等号进行了功能增强
	Student s5 = "K";
	// 打印 Student s4 实例对象值
	cout << "name : " << s5.m_name << " , age : " << s5.m_age << endl;

	// 调用拷贝构造函数
	Student s3 = Student(s2);
	// 打印 Student s3 实例对象值
	cout << "name : " << s3.m_name << " , age : " << s3.m_age << endl;



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

Results of the :

调用无参构造函数
name : name , age : 0
调用有参构造函数
name : Tom , age : 18
调用有参构造函数
name : J , age : 1
调用有参构造函数
name : K , age : 0
调用拷贝构造函数
name : Tom , age : 18
请按任意键继续. . .

Insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/132815469