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. How base class member method can output name/type of derived caller object ?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.4k Views 1 Watching
  • 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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?

      ODБOïO 1 Reply Last reply
      2
      • mrjjM mrjj

        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?

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          hi
          try with

          qDebug() << typeid(*this).name();
          
          ODБOïO 1 Reply Last reply
          4
          • mrjjM mrjj

            hi
            try with

            qDebug() << typeid(*this).name();
            
            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #5

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

            mrjjM 1 Reply Last reply
            0
            • ODБOïO ODБOï

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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @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 :)

              ODБOïO 1 Reply Last reply
              3
              • mrjjM mrjj

                @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 :)

                ODБOïO Offline
                ODБOïO Offline
                ODБOï
                wrote on last edited by
                #7

                @mrjj thx for help.

                1 Reply Last reply
                1

                • Login

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