toString() method equivalent for debugging
I come back from other language country like Python and Java Island... Both of them have a toString feature , which allow debugging class.
Is there something equivalent that I didn't see before ?
As example, I want to do :
MyClass car; qDebug()<<car;
For that kind of debugging you would usually implement a custom QDebug stream operator like described here
yes, but this is ugly.... I have to implement it outside the class.. That's mean create a MyClass::toString() and call it inside the QDebug operator... So, I prefer to write :
From a design point of view it's not ugly. It's actually prettier.
For example if you have a class Triangle or APieceOfRock they have conceptually no debugging or to string conversion notion whatsoever.
Why should a triangle or a piece of rock know anything about strings or how to convert to them? Should it also convert to other data types like ACar or QWidget? Should you add a conversion method to them every time you introduce another type in your app? Making
toSting()method in every class is pumping in it functionality that is out of their purpose scope and does not belong to that class.
A separate utility function, detached from the class is actually a much cleaner solution that keeps responsibility separation.