相关疑难解决方法(0)

使构造函数显式是一个很好的实践

在设计公共API时,将构造函数设置为显式是一种好的做法吗?

class A {
public:
    //explicit A(int i){}
    A(int i){}
};

void fun(const A& a) {}

int main() {
    // If I use explicit for A constructor, I can prevent this mistake.
    // (Or shall I call it as feature?)
    fun(10);
}
Run Code Online (Sandbox Code Playgroud)

或者我应该允许隐式转换,以允许用户以较少的输入调用我的API?

c++

8
推荐指数
3
解决办法
802
查看次数

标签 统计

c++ ×1