我正在尝试使用以下函数为字符串和双定义运算符 +
string operator + (const double& b,const string a){
return to_string(b)+a;
}
Run Code Online (Sandbox Code Playgroud)
当我进行以下操作时,效果很好
double c = 100.256;
string d = "if only";
cout<<c+d<<"\n";
Run Code Online (Sandbox Code Playgroud)
但是当我传递 const char 而不是 string 时,它会抛出编译错误('double'和'const char [4]'类型的无效操作数到二进制'operator+')
double c = 100.256;
string test = c+"sff";
Run Code Online (Sandbox Code Playgroud)
为什么不发生 const char[] "sff" 到字符串的隐式转换?
c++ operator-overloading operator-keyword explicit-conversion