显式非单参数构造函数 [英] explicit non-single parameter constructor

查看:154
本文介绍了显式非单参数构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释为什么非单参数构造函数标记为显式编译?
根据我的理解,这是绝对无用的关键字这里,为什么这个编译没有错误?

Can anyone explain why does non-single parameter constructor marked as explicit compile? As far as I understand this is absolutely useless keyword here, so why does this compile without error?

class X
{
public:
    explicit X(int a, int b) { /* ... */}
};


推荐答案

在C ++ 03中, ,两个参数构造函数标记为 explicit 没有任何意义。但这里可能有意义:

In C++03, and in this particular case, it makes no sense for a two parameter constructor to be marked explicit. But it could make sense here:

explicit X(int i, int j=42);

因此,用显式标记一个双参数构造函数不必是一个错误。

So, marking a two parameter constructor with explicit does not have to be an error.

在C ++ 11中,这种使用显式会阻止你这样做:

In C++11, this use of explicit would prevent you from doing this:

X x = {1,2};

这篇关于显式非单参数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆