how to add QDebug to inherited classes?
-
Hey
Solution below thanks to > @Christian-Ehrlicher
Kinda lost here...
Say I have:
class base { QString mBase; public: base() {}; ~base() {}; friend QDebug operator<<(QDebug &stream, const base &obj); }; QDebug operator<<(QDebug &stream, const base &obj) { stream << obj.mBase; return stream; } class inh : public base { QString mInh; public: inh() {}; ~inh() {}; friend QDebug operator<<(QDebug &stream, const inh &obj); }; QDebug operator<<(QDebug &stream, const inh &obj) { stream << obj.mInh;//"How do I call debug on base class here ?" return stream; }
How to properly get inherited debug ?
Ideally, I'd like something like this : - unless that's stupid and I missed elephant?
QDebug operator<<(QDebug &stream, const inh &obj) { stream << obj.baseClassDebug; stream << obj::base; // or ? stream stream << obj.mInh; return stream; }
TIA
Working example :
class base { QString mBase; public: base() : mBase("BASE CLASS") {}; ~base() {}; friend QDebug operator<<(QDebug &stream, const base &obj); }; QDebug operator<<(QDebug &stream, const base &obj) { stream << obj.mBase; return stream; } class other { QString mBase; public: other() : mBase("SomeOtherClass") {}; ~other() {}; friend QDebug operator<<(QDebug &stream, const other &obj); }; QDebug operator<<(QDebug &stream, const other &obj) { stream << obj.mBase; return stream; } class inh : public base { QString mInh; public: inh() : mInh("SINGLE INHERITED") {}; ~inh() {}; friend QDebug operator<<(QDebug &stream, const inh &obj); }; QDebug operator<<(QDebug &stream, const inh &obj) { const base &b = obj; stream << b; stream << obj.mInh; return stream; } class dual : public base, public other { QString mInh; public: dual() : mInh("DUAL INHERITED") {}; ~dual() {}; friend QDebug operator<<(QDebug &stream, const dual &obj); }; QDebug operator<<(QDebug &stream, const dual &obj) { const base &b = obj; const other &o = obj; stream << b; stream << o; stream << obj.mInh; return stream; } inh h; qDebug() << h; dual d; qDebug() << d;
-
@Dariusz
I don't know, but does https://stackoverflow.com/questions/2677577/how-to-overload-operator-for-qdebug help you? It even has an answer there from our friend @raven-worx here, https://forum.qt.io/topic/34523/solved-overloading-qdebug-stream-operation ! -
Yeah I saw the QString option and other one. However it feels a bit "wrong"... That would mean that all types/specific stuff I would have to rewrite from existing QDebug overloads to QString overloads... Kinda "ok" to do but I thought there I a proper QDebug way of doing it ?
This feels a bit "hacky"... like hey Use the QDebug, but don't use it for anything a little more complex? not sure...
-
This is a plain C++ problem
QDebug operator<<(QDebug &stream, const inh &obj) { const base &b = obj; stream << b; stream << obj.mInh; return stream; }
-
@Christian-Ehrlicher said in how to add QDebug to inherited classes?:
This is a plain C++ problem
QDebug operator<<(QDebug &stream, const inh &obj) { const base &b = obj; stream << b; stream << obj.mInh; return stream; }
This looks quite awesome!
I've updated 1st post with multiple inheritances working example. Hope it helps in future.
-
Ok I jumped the gun... - naturally...
So the solution above works great... but what can I do about this example?
inh *h = new inh(); qDebug() << *h; dual *d = new dual(); qDebug() << *d; base *b = d; qDebug() << *b;
It prints :
"BASE CLASS" "SINGLE INHERITED" "BASE CLASS" "SomeOtherClass" "DUAL INHERITED" "BASE CLASS"
it should print :
"BASE CLASS" "SINGLE INHERITED" "BASE CLASS" "SomeOtherClass" "DUAL INHERITED" "BASE CLASS" "SomeOtherClass" "DUAL INHERITED"
Any ideas how to deal with this?
The main issue I got is that
QDebug overload<<(QDebug source,const base&obj) is a const class... if only we could do a
QDebug overload<<(QDebug source,base&obj)
then I could do obj.myDebug()
where as virtual QDebug myDebug() could do :virtual QDebug myDebug(){ QDebug s; s << aseClass::myDebug() s<< "Extra pars"; return s }
If that is "possible"... but then again the main QDebug overload is const so I can't call inside class... mhmmm ?
And it has to be const base&obj because else QVector<base*> won't work... -
Since you're explicitly calling QDebug operator<<(QDebug &stream, const base &obj) the compiler can't call the other function.
What you can do is to write your own "virtual QString debug() const" function in every class which you the call within the debug stream operator function. But once again - plain C++ :)