explicit non-single parameter constructor

心已入冬 提交于 2020-01-03 09:04:02

问题


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) { /* ... */}
};

回答1:


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.

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

X x = {1,2};



回答2:


Not entirely true.

In C++11, constructors with multiple arguments can be implicitly converted using brace initialisation.



来源:https://stackoverflow.com/questions/20244070/explicit-non-single-parameter-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!