Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDebug, inheritance QObject issue ambiguous call
QtWS25 Last Chance

QDebug, inheritance QObject issue ambiguous call

Scheduled Pinned Locked Moved Unsolved General and Desktop
qdebug
6 Posts 2 Posters 1.1k Views
  • 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 Offline
    D Offline
    Dariusz
    wrote on 31 Aug 2020, 16:48 last edited by Dariusz
    #1

    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
    0
    • J Online
      J Online
      jeremy_k
      wrote on 31 Aug 2020, 17:22 last edited by
      #2

      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
      3
      • D Offline
        D Offline
        Dariusz
        wrote on 31 Aug 2020, 18:52 last edited by
        #3

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

        J 1 Reply Last reply 31 Aug 2020, 19:15
        0
        • D Dariusz
          31 Aug 2020, 18:52

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

          J Online
          J Online
          jeremy_k
          wrote on 31 Aug 2020, 19:15 last edited by
          #4

          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 31 Aug 2020, 19:31
          0
          • J jeremy_k
            31 Aug 2020, 19:15

            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;
            }
            
            D Offline
            D Offline
            Dariusz
            wrote on 31 Aug 2020, 19:31 last edited by
            #5

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

            J 1 Reply Last reply 31 Aug 2020, 19:36
            0
            • D Dariusz
              31 Aug 2020, 19:31

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

              J Online
              J Online
              jeremy_k
              wrote on 31 Aug 2020, 19:36 last edited by
              #6

              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
              0

              1/6

              31 Aug 2020, 16:48

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved