小编Add*_*ict的帖子

在c ++中使用隐式转换进行复制初始化

class Foo {
  public:
    Foo(float b) {}
};

class Bar {
  public:
    Bar(Foo foo) {}
};

int main(int argc, char *argv[]) {
    Bar b1(3.0f);  // accept, one implicit convertion happens there.
    Bar b2 = 3.0f;  // error: no viable conversion from 'float' to 'Bar'
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么第二个表达式无法编译?我希望它会调用与第一个表达式相同的转换构造函数.

c++ initialization

6
推荐指数
1
解决办法
302
查看次数

标签 统计

c++ ×1

initialization ×1