toString override in C++ [duplicate]
Asked Answered
O

4

45

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?

Omophagia answered 2/3, 2011 at 18:28 Comment(2)
You shouldn't dynamically allocate the Object with new there (it won't compile).Wasp
in C++, use either Object* x = new Object(); cout << *x; or Object x; cout << x;Piece
M
65
std::ostream & operator<<(std::ostream & Str, Object const & v) { 
  // print something from v to str, e.g: Str << v.getX();
  return Str;
}

If you write this in a header file, remember to mark the function inline: inline std::ostream & operator<<(... (See the C++ Super-FAQ for why.)

Maypole answered 2/3, 2011 at 18:29 Comment(3)
Note that this part is NOT in the class / struct!Kapoor
The answer about C++ inline functions in the header by @jalf here on SO seems to more clearly answer why to mark function definitions in the header inline than the FAQ did. - https://mcmap.net/q/149393/-why-do-inline-functions-have-to-be-defined-in-a-header-fileComeuppance
I get error: binary 'operator <<' has too many parametersCleavage
C
43

Alternative to Erik's solution you can override the string conversion operator.

class MyObj {
public:
    operator std::string() const { return "Hi"; }
}

With this approach, you can use your objects wherever a string output is needed. You are not restricted to streams.

However this type of conversion operators may lead to unintentional conversions and hard-to-trace bugs. I recommend using this with only classes that have text semantics, such as a Path, a UserName and a SerialCode.

Crozier answered 2/3, 2011 at 18:35 Comment(4)
However, implicit conversion operators like this one can lead to unpleasant surprises.Branle
It's better to define both, and have ostream& operator<< use the string operator. Another thing I would do is renaming the string operator to something like ToString() member function, reserving string cast for the case when the object is itself kind of a string.Piece
Avoid implicit conversion operators for non-directly-related types. They have a tendency to be the source of some very hard to find bugs if used on types that cannot meaningfully be converted to a specific type.Weizmann
Points taken and added to the answer.Crozier
V
6
 class MyClass {
    friend std::ostream & operator<<(std::ostream & _stream, MyClass const & mc) {
        _stream << mc.m_sample_ivar << ' ' << mc.m_sample_fvar << std::endl;
    }

    int m_sample_ivar;
    float m_sample_fvar;
 };
Vandalism answered 2/3, 2011 at 19:7 Comment(0)
R
2

Though operator overriding is a nice solution, I'm comfortable with something simpler like the following, (which also seems more likely to Java) :

char* MyClass::toString() {
    char* s = new char[MAX_STR_LEN];
    sprintf_s(s, MAX_STR_LEN, 
             "Value of var1=%d \nValue of var2=%d\n",
              var1, var2);
    return s;
}
Radiotelegraphy answered 6/2, 2016 at 0:54 Comment(3)
This function won't get called when you use std::cout << myClassInst;Dufrene
As I've stated - it's more likely to Java ... i.e.: It's for usage like: printf("myClassInst = %s\n", myClassInst.toString() ) ; or like: std::cout << myClassInst.toString() ; Radiotelegraphy
This is leaking memoryFreestyle

© 2022 - 2024 — McMap. All rights reserved.