Article directory
1. Constructor issues when class objects are used as member variables
1. Problem description
If an object of class A is used as a member variable of another class B,
An error will be reported in the following scenarios:
- Define a parameterized constructor for class A, then the parameterless default constructor of A will be invalid;
- At this time, if you use the default parameterless constructor to initialize B, an error will be reported;
In a class, its member variables are of a type with a parameterized constructor. In this case, there is no chance to call the parameterized constructor, and a compilation error will occur;
In the following code, A can only be initialized through the parameterized constructor A(int age, int height)
, and the parameterless constructor can no longer be used;
class A
{
public:
// 带参构造函数
A(int age, int height)
{
}
public:
int m_age; // 年龄
int m_height; // 身高
};
In B, declare the member variable of A as follows. You can only call the parameterless constructor of A to create the A object. However, the parameterless constructor of A cannot be used. You must use the parameterized constructor of A. A problem occurs here and an error is reported “B::B(void)”: 由于 数据成员“B::m_a”不具备相应的 默认构造函数 或重载解决不明确,因此已隐式删除函数
;
class B
{
public:
int m_age; // 年龄
A m_a; // A 类型成员变量
};
The solution to the above problem is the constructor initialization list in C++;
2. Error code example
Code example:
#include "iostream"
using namespace std;
class A
{
public:
// 带参构造函数
A(int age, int height)
{
}
public:
int m_age; // 年龄
int m_height; // 身高
};
class B
{
public:
int m_age; // 年龄
A m_a; // A 类型成员变量
};
int main()
{
// 通过 B 的默认无参构造函数初始化 B 对象
B b;
// 控制台暂停 , 按任意键继续向后执行
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(28): error C2280: “B::B(void)”: 尝试引用已删除的函数
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(22): message : 编译器已在此处生成“B::B”
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(22,1): message : “B::B(void)”: 由于 数据成员“B::m_a”不具备相应的 默认构造函数 或重载解决不明确,因此已隐式删除函数
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(21): message : 参见“B::m_a”的声明
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
2. Constructor initialization list
1. Constructor initialization list syntax rules
In the C++ language, the constructor initialization list is a method used to initialize member variables of a class;
The constructor initialization list can implement functions:
- Provide initial values for member variables
- Call the constructor of other member variables to initialize member variables
Constructor initialization list syntax rules:
构造函数() : 成员变量名称(成员变量值) , 成员变量名称(成员变量值)
{
// 构造函数内容
}
- The constructor initialization list is located after the parameter list of the constructor, between the colon
:
and the curly braces{}
; - Use commas
,
to separate; - The elements in the initialization list consist of the name and initial value of the member variable,
=
connected using the equal sign;
In the following code, a default constructor is defined for class B, which defines a constructor initialization list;
In the initialization list,
m_age(10)
It provides the initial value for m_age;m_a(10, 150)
The parameterized constructor of A is called;
Code example:
class B
{
public:
B() : m_age(10) , m_a(10, 150)
{
}
public:
int m_age; // 年龄
A m_a; // A 类型成员变量
};
2. Code example - Constructor initialization list syntax rules
Code example:
#include "iostream"
using namespace std;
class A
{
public:
// 带参构造函数
A(int age, int height)
{
m_age = age;
m_height = height;
}
public:
int m_age; // 年龄
int m_height; // 身高
};
class B
{
public:
B() : m_age(10) , m_a(10, 150)
{
}
public:
int m_age; // 年龄
A m_a; // A 类型成员变量
};
int main()
{
// 通过 B 的默认无参构造函数初始化 B 对象
B b;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
}
Results of the :