C++ toString 运算符类似于 Object.toString

C++ toString operator similair to Object.toString

在Java中,每个Object都有一个toString方法和一个hashcode方法。 C++ 中的每个对象是否有等效的 hashcode 和 toString?

语言中没有这样的东西。并非 C++ 中的所有内容都是对象(没有通用的 class 可以派生所有内容)。

没有对应的。与 JAVA 不同,并非 C++ 中的所有内容都派生自某些 (Object) 超类。没有 ::toString() 成员函数,因为 C++ 中没有超类。 C++也不支持反射。

也就是说,每个 类 都有一个 std::to_string function having 9 different overloads for the built-in types. To achieve the functionality you want, you can overload the output stream operator<<