Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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.

    https://en.cppreference.com/w/cpp/language/friend note #2



  • @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 did qDebug()<<(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;
    }
    


  • @jeremy_k Ohh I see, so re-implement debug call inside inherited class and do a cast there to correct type/etc/etc... nice! Will give it a go, thanks!



  • Anything to make one definition more specific should work. My ability to recall the full set of rules is clearly less than perfect.


Log in to reply