类型转换
一、自定义类型转基本类型 如果想把自定义类型转为基本类型,则需要重载类型转换符,否则就会报错,无法完成转换。 class TypeTest { public: TypeTest() { int_type = 0; double_type = 0.0; } operator int() const //重载int { return int_type; } operator double() const //重载double { return double_type; } private: int int_type = 0; double double_type = 0; }; int main() { TypeTest t1; int t2 = (int)t1; cout << t2 << endl; return 0; } 来源: CSDN 作者: Ftworld21 链接: https://blog.csdn.net/Ftworld21/article/details/103585558