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. typeid meets heritage

typeid meets heritage

Scheduled Pinned Locked Moved Solved General and Desktop
typeid
7 Posts 3 Posters 2.4k Views 3 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.
  • W Offline
    W Offline
    Walux
    wrote on last edited by
    #1

    Hi Qt Community :)

    Sorry for the lazy post but ,
    i spent a long time searching for a solution to this simple issue :

    if(typeid(*(element)) == typeid(motherClass))
            doSmth();
    

    The typeid can only identify the motherClass instances , and not its childs (inheriting classes)

    How to fix this obvious issue ?
    Nice regards :)

    Taking things from beginning to end : That's my entertainment !

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

      Hi,

      What do you want to achieve ? Call doSmth if element is a derived class of motherClass ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      W 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What do you want to achieve ? Call doSmth if element is a derived class of motherClass ?

        W Offline
        W Offline
        Walux
        wrote on last edited by Walux
        #3

        @SGaist

        Yes , I want it called if element's derived of MotherClass , or it IS motherClass .

        Taking things from beginning to end : That's my entertainment !

        1 Reply Last reply
        0
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4
          if (dynamic_cast<motherClass*>(element))
              doSmth();
          

          or, in case of QObject derived classes

          if (qobject_cast<motherClass*>(element))
              doSmth();
          
          W 1 Reply Last reply
          0
          • Chris KawaC Chris Kawa
            if (dynamic_cast<motherClass*>(element))
                doSmth();
            

            or, in case of QObject derived classes

            if (qobject_cast<motherClass*>(element))
                doSmth();
            
            W Offline
            W Offline
            Walux
            wrote on last edited by Walux
            #5

            @Chris-Kawa

            Does this trick work even if my class is derived of both QObject , and QGraphicsitem ? Because compiler gives me that known warning that object_cast won't work unil i implement the type() method .

            Taking things from beginning to end : That's my entertainment !

            1 Reply Last reply
            0
            • Chris KawaC Online
              Chris KawaC Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              As I said - qobject_cast works only for QObject derived classes. QGraphicsItem does not derive from QObject. For that you need a dynamic_cast.

              W 1 Reply Last reply
              1
              • Chris KawaC Chris Kawa

                As I said - qobject_cast works only for QObject derived classes. QGraphicsItem does not derive from QObject. For that you need a dynamic_cast.

                W Offline
                W Offline
                Walux
                wrote on last edited by
                #7

                @Chris-Kawa

                Thank you very much , works like a charm :)

                Taking things from beginning to end : That's my entertainment !

                1 Reply Last reply
                0

                • Login

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