今天在研读google c++编码规范时,在构造函数一节中提到了explicit关键字,规范中要求“对单参数构造函数使用C++关键字explicit”。
原文定义为:
Normally, if a constructor takes one argument, it can be used as a conversion. Forinstance, if you define Foo::Foo(string name) and then pass a string to a function thatexpects a Foo, the constructor will be called to convert the string
into a Foo and will passthe Foo to your function for you. This can be convenient but is also a source of trouble whenthings get converted and new objects created without you meaning them to. Declaring aconstructor explicit prevents it from
being invoked implicitly as a conversion.
下面这段代码是编译不过去的。
#include <iostream>
#include <stdio.h>
using namespace std;
class CTest
{
public:
explicit CTest(const CTest& c) : m_i(c.m_i) /// copy constructor
{
printf("in the copy constructor(CTest).\n");
}
explicit CTest(int i) : m_i(i) /// constructor
{
printf("in the constructor(CTest).\n");
}
CTest() /// defalut constructor
{
printf("in the defalut constructor(CTest)\n");
}
explicit CTest &operator = (int i)
{
m_i = 1;
printf("in the assignment constructor(CTest)\n");
}
public:
int m_i; ///
};
CTest func(CTest &c)
{
c.m_i = 1;
return c;
}
int main()
{
CTest c1, c2;
c2 = func(c1);
printf("c1.m_i = %d, c2.m_i = %d\n", c1.m_i, c2.m_i);
return 0;
}
func函数将对象c通过值传递返回,隐式的调用了CTest的拷贝构造函数,但是CTest拷贝构造函数使用了explicit关键字,禁止了隐式转换的功能,如果将explicit关键字去掉就可以编译。
结果输出为
in the defalut constructor(CTest)
in the defalut constructor(CTest)
in the copy constructor(CTest).
c1.m_i = 1, c2.m_i = 1
还是这个类CTest,如果main()如下:
int main()
{
CTest c;
c = 1;
printf("c.m_i = %d\n", c.m_i);
return 0;
}
仍旧不能正常编译,原因是:
c = 1; 这句话在编译器看来是不正确的,但是编译器可以发现CTest可以根据一个int来构造,所以调用了构造函数,生成了一个临时的对象,然后将这个临时变量赋值给了c。
可以把这句话翻译成为:
CTest temp(1);
CTest c = temp;
可见,程序分别隐式的调用了明确构造函数和赋值构造函数,但是CTest类中的明确构造函数和赋值构造函数使用了explicit来修饰,导致程序无法正确编译,如果将这两个explicit去掉,则运行结果如下:
in the defalut constructor(CTest)
in the assignment constructor(CTest)
c.m_i = 1
总之, explicit关键字用来修饰构造函数,表面该构造函数是显示的,如果进行了构造函数的隐式操作,根据编译器的不同,有些会告警,而有些会发生编译错误。
感谢A725SASA 对本文错误的指正。
分享到:
相关推荐
以下是对C++中Explicit关键字的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助
在C++程序中很少有人去使用explicit关键字,不可否认,在平时的实践中确实很少能用的上,再说C++的功能强大,往往一个问题可以利用好几种C++特性去解决。接下来给大家介绍 C++中的explicit关键字,需要的朋友可以参考...
在C++程序中很少有人去使用explicit关键字,不可否认,在平时的实践中确实很少能用的上。再说C++的功能强大,往往一个问题可以利用好几种C++特性去解决。但稍微留心一下会发现现有的MFC库或者C++标准库中的相关类...
C++ explicit关键字用来修饰类的构造函数,表明该构造函数是显式的,既然有"显式"那么必然就有"隐式",那么什么是显示而什么又是隐式的呢?下面就让我们一起来看看这方面的知识吧
带单一参数的构造函数在缺省情况下隐含一个转换操作符,请看下面的代码: class MyClass{ public: MyClass(int nParam);// 带一个参数的构造函数 ... //////////////////////////////
C/C++中的volatile关键字和const对应,用来修饰变量,用于告诉编译器该变量值是不稳定的,可能被更改。使用volatile注意事项: (1). 编译器会对带有volatile关键字的变量禁用优化(A volatile specifier is a hint to...
C++ explicit关键字在刚学的时候就接触到了,也从各处了解了一些,但始终云里雾里,在闲来无事的时候再仔细研究了一下,自己消化理解了一下,自认为比其他网友总结的精简而不失准确。