当构造函数是显式的时,它不用于隐式转换。在给定的代码段中,构造函数被标记为explicit
。那为什么万一foo obj1(10.25);
它正在工作而foo obj2=10.25;
它不工作呢?
#include <iostream>
class foo
{
int x;
public:
explicit foo( int x ):x(x)
{}
};
int main()
{
foo obj(10.25); // Not an error. Why ?
foo obj2 = 10.25; // Error
getchar();
return 0;
}
错误:错误 C2440:“正在初始化”:无法从“双”转换为“富”