多参数构造函数上的显式关键字? [英] Explicit keyword on multi-arg constructor?

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

问题描述

我最近遇到了一些奇怪的类,有三个构造函数:

I recently came across some weird looking class that had three constructors:

class Class
{
    public:
        explicit Class(int );

        Class(AnotherClass );

        explicit Class(YetAnotherClass, AnotherClass );

    // ...
}

对我来说真的有意义 - 我认为显式关键字是保护编译器选择的结构从外部类型。

This doesn't really make sense to me - I thought the explicit keyword is to protect compiler chosen construction from a foreign type.

这是允许的吗?

推荐答案

在C ++ 11中,多参数构造函数可以隐式转换为括号

In C++11 multi-parameter constructors can be implicitly converted to with brace initialization.

但是,在C ++ 11 显式之前,仅应用于单参数构造函数。对于多参数构造函数,它被忽略,无效

However, before C++11 explicit only applied to single-argument constructors. For multiple-argument constructors, it was ignored and had no effect.

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

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