为什么C++会将字符串文字转换为bool而不是字符串?
#include <iostream>
using namespace std;
class A
{
public:
A(string v)
{
cout << v;
}
A(bool v)
{
cout << v;
}
};
int main()
{
A("hello");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出: 1
是因为编译器不够聪明,无法从char*跳转到字符串,而只是假设bool是最接近指针的东西?我唯一的选择是创建一个显式的char*构造函数,它基本上与字符串构造函数完全相同吗?