[转载] C++ 中 explicit 关键字 的用法

[转载] C++ 中 explicit 关键字 的用法

注:转载文章,侵删
来源:C++ 中 explicit 关键字 的用法

作者:Tainql
链接:https://www.jianshu.com/p/af8034ec0e7a


explicit的作用是用来声明类构造函数是显示调用的,而非隐式调用,所以只用于修饰单参构造函数。因为无参构造函数和多参构造函数本身就是显示调用的。再加上explicit关键字也没有什么意义。

explicit关键字的官方解释:

This keyword is a declaration specifier that can only be applied to in-class constructor declarations . An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object 。

从上面的解释可以看出explicit的出要作用是:

  • 只能用来修饰类构造函数
  • explicit修饰的构造函数不能被隐式调用
  • 禁止类对象之间的隐式转换

举例如下:

#include<cstring>
#include<string>
#include<iostream>

class Explicit
{
    private:

    public:
        Explicit(int size)
        {
            std::cout << " the size is " << size << std::endl;
        }
        Explicit(const char* str)
        {
            std::string _str = str;
            std::cout << " the str is " << _str << std::endl;
        }

        Explicit(const Explicit& ins)
        {
            std::cout << " The Explicit is ins" << std::endl;
        }

        Explicit(int a,int b)
        {
            std::cout << " the a is " << a  << " the b is " << b << std::endl;
        }
};

int main()
{
    Explicit test0(15);
    Explicit test1 = 10;// 隐式调用Explicit(int size)

    Explicit test2("RIGHTRIGHT");
    Explicit test3 = "BUGBUGBUG";// 隐式调用Explicit(const char* str)

    Explicit test4(1, 10);
    Explicit test5 = test1;
}

上面的程序虽然没有错误,但是对于Explicit test1 = 10;和Explicit test2 = "BUGBUGBUG";这样的句子,把一个int类型或者const char*类型的变量赋值给Explicit类型的变量看起来总归不是很好,并且当程序很大的时候出错之后也不容易排查。所以为了禁止上面那种隐式转换可能带来的风险,一般都把类的单参构造函数声明的显示调用的,就是在构造函数加关键字explicit。如下:

#include<cstring>
#include<string>
#include<iostream>

class Explicit
{
    private:

    public:
        explicit Explicit(int size)
        {
            std::cout << " the size is " << size << std::endl;
        }
        explicit Explicit(const char* str)
        {
            std::string _str = str;
            std::cout << " the str is " << _str << std::endl;
        }

        Explicit(const Explicit& ins)
        {
            std::cout << " The Explicit is ins" << std::endl;
        }

        Explicit(int a,int b)
        {
            std::cout << " the a is " << a  << " the b is " << b << std::endl;
        }
};

int main()
{
    Explicit test0(15);
    Explicit test1 = 10;// 无法调用

    Explicit test2("RIGHTRIGHT");
    Explicit test3 = "BUGBUGBUG"; // 无法调用

    Explicit test4(1, 10);
    Explicit test5 = test0;
}

上面再写Explicit test1=10; Explicit test3 = "BUGBUGBUG";这样的句子的时候程序就会报如下错误:

error: conversion from ‘int’ to non-scalar type ‘Explicit’ requested
error: conversion from ‘const char [10]’ to non-scalar type ‘Explicit’ requested


作者:Tianql
链接:https://www.jianshu.com/p/af8034ec0e7a
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


原文链接: c++ explicit 关键字

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值