explicit
前言
C++中, 一个类的构造函数(或者除了第一个参数外其余参数都有默认值的多参构造函数), 承担了两个角色。
1.是个构造器
2.是个默认且隐含的类型转换操作符。
0x00 explicit
C++中的关键字 explicit 主要是用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换。类构造函数默认情况下声明为隐式的即 implicit 。
explicit关键字作用于单个参数的构造函数,如果构造函数有多个参数,但是从第二个参数开始,如果各参数均有默认赋值,也可以应用explicit关键字防止隐式转换。
class VectorInt2D
{
public:
VectorInt2D()
{
Init();
}
explicit VectorInt2D(int x) // explicit防止隐式转换
{
Init(x);
}
VectorInt2D(int x, int y)
{
Init(x, y);
}
VectorInt2D(const VectorInt2D& obj)
{
this->x = obj.x;
this->y = obj.y;
// 重新申请新的内存空间
uint32_t nLen = strlen(obj.m_pszClassName) + 1;
m_pszClassName = new char[nLen];
strcpy_s(m_pszClassName, nLen, obj.m_pszClassName);
}
void Init(int x = 0, int y = 0)
{
uint32_t nLen = strlen("VectorInt2D") + 1;
m_pszClassName = new char[nLen];
strcpy_s(m_pszClassName, nLen, "VectorInt2D");
this->x = x;
this->y = y;
}
~VectorInt2D()
{
if (m_pszClassName != nullptr)
{
delete m_pszClassName;
m_pszClassName = nullptr;
}
}
char* GetClassName()
{
return m_pszClassName;
}
int GetX()
{
return this->x;
}
int GetY()
{
return this->y;
}
private:
char* m_pszClassName;
int x;
int y;
};
void ShowPoint(VectorInt2D pos)
{
printf("%s x=%d y=%d\n", pos.GetClassName(), pos.GetX(), pos.GetY());
}
int main(int argc, char* argv[])
{
VectorInt2D pos1(1, 2);
// 参数为对象自动匹配构造函数 这里匹配拷贝构造
ShowPoint(pos1);
// 显式调用构造函数
VectorInt2D pos2(2);
//编译错误,不能隐式调用其构造函数
ShowPoint(1); // error C2664: “void ShowPoint(VectorInt2D)”: 无法将参数 1 从“int”转换为“VectorInt2D”
return 0;
}
如果构造函数explicit VectorInt2D(int x)
中去掉 explicit 关键字,则ShowPoint(1);
可以调用成功。这时会发生隐式转换,用 1 构造 VectorInt2D 对象,然后传参给 ShowPoint 函数。