How base class member method can output name/type of derived caller object ?



  • Hello,

    I have a base class QOpcUaClient with a member method called connect()

    QOpcUaClient::connect(){
     qDebug()<<"Connecting : " << /*and hree i want to output name of the derived object using this method */;
    }
    

    In main.cpp I have 3 classes derived from that QOpcUaClient
    cmdClient / moveClient / subClient

    CmdClient cmd;
    cmd.connect();
    
    MoveClient move;
    move.connect();
    
    SubClient sub;
    sub.connect();
    
    

    How my 'connect()' method can output name/type of caller object ?
    I can pass the name by parameter, but i want to know if it is possible without.

    Thank you in advance.

    LA


  • Qt Champions 2017

    Hi
    You mean the name of the class/instance that calls base class method?
    You can make connect virtual and manually call the base version.
    The caller will then be the subclass. so "this" will be the actual sub class.

    But why - if i may ask?



  • @mrjj Hi,

    @mrjj said

    You mean the name of the class/instance that calls base class method?

    Yes !

    @mrjj said

    You can make connect virtual and manually call the base version.

    yes, but as my 3 clients have the exact same code for connection I don't realy need 'connect()' to be virtual..

    Why ? just for for debugging purposes : for the moment, when one of this 3 objects call 'connect()' i have only the output :
    Connecting :

    I want to see : Connecting cmd or Connecting sub ..

    Thx


  • Qt Champions 2017

    hi
    try with

    qDebug() << typeid(*this).name();
    


  • @mrjj said in How base class member method can output name/type of derived caller object ?:

    typeid(*this).name();

    Perfect ! thank you.

    just a little precision, i have this output :

    c++ : connecting : 13Subscriptions
    c++ : connecting : 7Command
    c++ : connecting : 9Mouvement

    what are this 13 / 7 / 7 ?


  • Qt Champions 2017

    @LeLev
    As far as i recall its the internal hash code. ( like internal ID)
    the name() function might add it to the output.
    You can see if qDebug() << typeid(*this).hash_code(); gives that id.

    Update: Could also just be string length :)



  • @mrjj thx for help.


Log in to reply
 

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