Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. typeid in a tempate derived class

typeid in a tempate derived class

Scheduled Pinned Locked Moved Unsolved C++ Gurus
typeidtemplate
6 Posts 2 Posters 1.4k 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.
  • A Offline
    A Offline
    addebito
    wrote on 10 Nov 2019, 23:08 last edited by addebito 11 Oct 2019, 23:08
    #1

    Hi guys,
    I'm new in C++ and I don't know how to solve this problem.
    I've gooled a lot and I found the "typeid" or "decltype" keyword but I don't understand exactly how to use it.
    I try to explain my problem by example.

    class Base
    {
    public:
      Base() {};
    }
    
    template <class T>
    class Derived: public Base
    {
    public:
      Derived(): Base() {};
      T *value = nullptr;
    }
    
    .....
    //on main I've a pointer to base class
    Base* b;
    // ...the object allocated is a Derived object but I don't know the type <T>
    
    // I can print the type
    qDebug() << typeid(b).name();
    
    // I can static_cast the base pointer to derived class like this
    Derived<int>* my_derived_class = static_cast<Derived<int>*>(b);
    qDebug() << *my_derived_class->value;
    
    // but if I don't know the type T??
    // How to get and use it ???
    Derived<???>* my_derived_class = static_cast<Derived<???>*>(b);
    
    

    Any advice and suggestions will be greatly appreciated.
    Thank you for your time.

    C 1 Reply Last reply 11 Nov 2019, 19:51
    0
    • A addebito
      10 Nov 2019, 23:08

      Hi guys,
      I'm new in C++ and I don't know how to solve this problem.
      I've gooled a lot and I found the "typeid" or "decltype" keyword but I don't understand exactly how to use it.
      I try to explain my problem by example.

      class Base
      {
      public:
        Base() {};
      }
      
      template <class T>
      class Derived: public Base
      {
      public:
        Derived(): Base() {};
        T *value = nullptr;
      }
      
      .....
      //on main I've a pointer to base class
      Base* b;
      // ...the object allocated is a Derived object but I don't know the type <T>
      
      // I can print the type
      qDebug() << typeid(b).name();
      
      // I can static_cast the base pointer to derived class like this
      Derived<int>* my_derived_class = static_cast<Derived<int>*>(b);
      qDebug() << *my_derived_class->value;
      
      // but if I don't know the type T??
      // How to get and use it ???
      Derived<???>* my_derived_class = static_cast<Derived<???>*>(b);
      
      

      Any advice and suggestions will be greatly appreciated.
      Thank you for your time.

      C Online
      C Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on 11 Nov 2019, 19:51 last edited by
      #2

      // but if I don't know the type T??
      // How to get and use it ???

      C++ is a strongly typed language. If you don't know the type you can't use it (properly).
      Think about it from the CPU's perspective - b is some address in memory. There's no way to know what concrete type it is. The only one who can know is you and you're the one telling the CPU what it is with a cast.

      But how are you suppose to know, you might ask. Well, a common way to do it is via some sort of type tagging. You can see this in action e.g. in QEvent,where it has a type() method that lets you know what concrete type it is so you can do stuff like

      if (evt->type() == QEvent::MouseMove) {
          QMouseEvent* mouse_evt = static_cast<QMouseEvent*>(evt);
         // do something with mouse_evt
      }
      
      A 1 Reply Last reply 11 Nov 2019, 21:06
      3
      • C Chris Kawa
        11 Nov 2019, 19:51

        // but if I don't know the type T??
        // How to get and use it ???

        C++ is a strongly typed language. If you don't know the type you can't use it (properly).
        Think about it from the CPU's perspective - b is some address in memory. There's no way to know what concrete type it is. The only one who can know is you and you're the one telling the CPU what it is with a cast.

        But how are you suppose to know, you might ask. Well, a common way to do it is via some sort of type tagging. You can see this in action e.g. in QEvent,where it has a type() method that lets you know what concrete type it is so you can do stuff like

        if (evt->type() == QEvent::MouseMove) {
            QMouseEvent* mouse_evt = static_cast<QMouseEvent*>(evt);
           // do something with mouse_evt
        }
        
        A Offline
        A Offline
        addebito
        wrote on 11 Nov 2019, 21:06 last edited by
        #3

        @Chris-Kawa
        I can do:

        if (dynamic_cast<Derived<double>*>(b))
          qDebug() << "generics double class";
        else if (dynamic_cast<Derived<int>*>(b))
          qDebug() << "generics int class";
        else
          qDebug() << "unknown class type";
        

        There is no other way instead use a if... else if... else if..... for any types ???

        1 Reply Last reply
        0
        • C Online
          C Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on 11 Nov 2019, 21:10 last edited by
          #4

          There is no other way instead use a if... else if... else if..... for any types ???

          No, but I'm not sure what are you trying to achieve. What do you want to do with that thing after the cast? Maybe a virtual function is what you're looking for? Can you give a specific example of types and what you want to do with them after the cast?

          A 1 Reply Last reply 11 Nov 2019, 21:35
          1
          • C Chris Kawa
            11 Nov 2019, 21:10

            There is no other way instead use a if... else if... else if..... for any types ???

            No, but I'm not sure what are you trying to achieve. What do you want to do with that thing after the cast? Maybe a virtual function is what you're looking for? Can you give a specific example of types and what you want to do with them after the cast?

            A Offline
            A Offline
            addebito
            wrote on 11 Nov 2019, 21:35 last edited by
            #5

            @Chris-Kawa
            Yes of course, you are right.
            After the cast I need to set the "value" pointer.

            Base* b1;
            Base* b2;
            //...
            Derived<int>* my_d1 = static_cast<Derived<int>*>(b1);
            Derived<int>* my_d2 = static_cast<Derived<int>*>(b2);
            // I'm linking the value pointer of b2 class to the value pointer of b1 class.
            my_d2->value = my_d1->value;
            
            C 1 Reply Last reply 11 Nov 2019, 22:35
            0
            • A addebito
              11 Nov 2019, 21:35

              @Chris-Kawa
              Yes of course, you are right.
              After the cast I need to set the "value" pointer.

              Base* b1;
              Base* b2;
              //...
              Derived<int>* my_d1 = static_cast<Derived<int>*>(b1);
              Derived<int>* my_d2 = static_cast<Derived<int>*>(b2);
              // I'm linking the value pointer of b2 class to the value pointer of b1 class.
              my_d2->value = my_d1->value;
              
              C Online
              C Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on 11 Nov 2019, 22:35 last edited by
              #6

              The only way I see to write this "genericly" would be to have some virtual QVariant getValue() const and virtual QVariant setValue(QVariant v) in your base class and implement it in Derived.
              This way you wouldn't have to do any casting, simply b2->setValue(b1->getValue()).
              The implementation in Derived would do the cast to T via QVariant's value().

              1 Reply Last reply
              2

              6/6

              11 Nov 2019, 22:35

              • Login

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