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 ?
QtWS25 Last Chance

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.3k 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.
  • O Offline
    O Offline
    ODБOï
    wrote on 20 Apr 2018, 08:54 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 20 Apr 2018, 09:08 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?

      O 1 Reply Last reply 20 Apr 2018, 09:41
      2
      • M mrjj
        20 Apr 2018, 09:08

        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?

        O Offline
        O Offline
        ODБOï
        wrote on 20 Apr 2018, 09:41 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
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 20 Apr 2018, 11:16 last edited by
          #4

          hi
          try with

          qDebug() << typeid(*this).name();
          
          O 1 Reply Last reply 20 Apr 2018, 12:50
          4
          • M mrjj
            20 Apr 2018, 11:16

            hi
            try with

            qDebug() << typeid(*this).name();
            
            O Offline
            O Offline
            ODБOï
            wrote on 20 Apr 2018, 12:50 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 ?

            M 1 Reply Last reply 20 Apr 2018, 14:27
            0
            • O ODБOï
              20 Apr 2018, 12:50

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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 20 Apr 2018, 14:27 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 :)

              O 1 Reply Last reply 23 Apr 2018, 10:33
              3
              • M mrjj
                20 Apr 2018, 14:27

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

                O Offline
                O Offline
                ODБOï
                wrote on 23 Apr 2018, 10:33 last edited by
                #7

                @mrjj thx for help.

                1 Reply Last reply
                1

                1/7

                20 Apr 2018, 08:54

                • Login

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