qDebug overload, return non const function?



  • Hey

    I'm trying to overload qDebug but I struggle to solve some of the things. The qDebug () friend class is located in base class, and then I wish to output data via virtual function to get the inherited data output... In any case here is the test :

        virtual QDebug getDebugData(QDebug stream){ // if I add )const{ then the error1 below goes away but I get new error2 
            stream <<"inherited data?";
            return stream;
        }
        
        friend QDebug operator<<(QDebug stream, const testObject &object) {
            stream << object.getDebugData(stream); // error1 function is not marked const. // error2 if above is const then this flags as "Invalid operands to binary expression ('QDebug' to 'QDebug')
            stream <<"this Is base class"; 
            return stream;
        }
    

    I kinda get the issue with const but I dont. I struggle with them lately. Would any1 mind helping me out with this?

    TIA


  • Qt Champions 2018

    Your getDebugData() should return a QString and should be marked as const since you're working on a const object inside operator<<()



  • Awesome thanks! That worked :- )

    But I run in to a wall, the virtual QString getDebugData never actually calls the upper inherited classes getDebugData function. I just end up with base class debug even tho I debug()<<inheritedObject; Any hints as to why that would not work?

    Regards
    Dariusz
    TIA


  • Qt Champions 2018

    @Dariusz said in qDebug overload, return non const function?:

    the virtual QString getDebugData never actually calls the upper inherited classes getDebugData

    I don't understand - what do you mean here? I don't see that you call the base class implementation in this function anywhere so how should it be called?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.