在设计公共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++ ×1