【发布时间】:2015-09-06 14:15:12
【问题描述】:
请看这段代码:
#include <iostream>
class A {
public:
int my;
A(int a=0) : my(a) { }
};
int main() {
A x = 7; // 1
A y = 6.7; // 2
std::cout << x.my << " " << y.my << "\n";
}
虽然没有 A(double a); 构造函数,但它实际上可以编译。
究竟什么时候允许编译器将一种参数类型转换为另一种来调用相应的构造函数?
【问题讨论】:
-
When exactly compiler is allowed to convert每次[当隐式转换是可能的并且没有更好的方法时]。 -
This reference 列出标准转换。这里特别感兴趣的是浮点积分转换。
-
另外,查看构造
A x(7)和赋值x = 7;之间的区别
标签: c++ constructor type-conversion