C++中的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 函数。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shlyyy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值