Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QDebug, inheritance QObject issue ambiguous call

    General and Desktop
    qdebug
    2
    6
    545
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      Dariusz last edited by Dariusz

      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

      1 Reply Last reply Reply Quote 0
      • jeremy_k
        jeremy_k last edited by

        C++ friendship isn't inherited.

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

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply Reply Quote 3
        • D
          Dariusz last edited by

          @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.

          jeremy_k 1 Reply Last reply Reply Quote 0
          • jeremy_k
            jeremy_k @Dariusz last edited by

            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;
            }
            

            Asking a question about code? http://eel.is/iso-c++/testcase/

            D 1 Reply Last reply Reply Quote 0
            • D
              Dariusz @jeremy_k last edited by

              @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!

              jeremy_k 1 Reply Last reply Reply Quote 0
              • jeremy_k
                jeremy_k @Dariusz last edited by

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

                Asking a question about code? http://eel.is/iso-c++/testcase/

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post