explicit关键字

今天在研读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 对本文错误的指正。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值