QDebug, inheritance QObject issue ambiguous call
-
Hey
So this one is a bit unexpected. I decided to do another refactor of my class debug system. The base class works fine, but when it gets to printing inherited ones... it kinda flips...
Here is some code :class base { public: base(); ~base(); friend QDebug operator<<(QDebug stream, const base*object); } class inherA : public QObject, public base{ public inherA(); ~inherA(); }
say I have
auto *class = inherA(); qDebug()<<class;
I'll get issue as c++ don't know which debug it should go for.... the native Qt one, or the one I implemented...
Woahz... any ideas on how to bite it?
TIA
-
C++ friendship isn't inherited.
-
@Dariusz said in QDebug, inheritance QObject issue ambiguous call:
friend QDebug operator<<(QDebug stream, const base*object);
Hmmmm thats good to know, but not sure if thats the issue?
This is the problem
base class:
friend QDebug operator<<(QDebug stream, const base*object);
Qt:
QDebug operator<<(QDebug stream, const QObject*object);
They both are viable options for the inheritedA class, and thust cause issue, for now I didqDebug()<<(base*)class;
instead and that seems to work, but I do wonder if there is a more "proper" way of doing it. -
I was under the impression that friendship played a role in overload resolution, but am not finding a reference in https://en.cppreference.com/w/cpp/language/overload_resolution
Casting works. static_cast is generally preferred over the C-style (Type *).
Another option is to create an intermediate class that is closer to the final inherited class.
class Derived: public QObject, public base {}; QDebug operator<<(QDebug stream, const * Derived d) { return operator<<(stream, const_cast<const base *>(d)); } class inherA: public Derived; void test() { inherA instance; qDebug() << instance; }