In Java, when a class overrides .toString()
and you do System.out.println()
it will use that.
class MyObj {
public String toString() { return "Hi"; }
}
...
x = new MyObj();
System.out.println(x); // prints Hi
How can I accomplish that in C++, so that:
Object x = new Object();
std::cout << *x << endl;
Will output some meaningful string representation I chose for Object
?
new
there (it won't compile). – WaspObject* x = new Object(); cout << *x;
orObject x; cout << x;
– Piece