最近,我浏览了 O'Reilly Media 的C++ Pocket Reference副本,当我遇到一个关于用户定义类型的用户定义转换的简短部分和示例时,我感到很惊讶:
#include <iostream>
class account {
private:
double balance;
public:
account (double b) { balance = b; }
operator double (void) { return balance; }
};
int main (void) {
account acc(100.0);
double balance = acc;
std::cout << balance << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用C++编程了一段时间,这是我第一次看到这种运算符重载.这本书对这个主题的描述有些简短,给我留下了一些关于这个功能的未解答的问题:
是否可以完成用户定义的用户定义转换?例如
operator std :: string(){/*code*/}