DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: C++, RTTI and vectors; getting the exact type

  1. #1
    Join Date
    Mar 2009
    Posts
    14

    C++, RTTI and vectors; getting the exact type

    Hello,

    I have a base class that derives two childs.

    class A {};
    class B : public A {};
    class C : public A {};

    I have another class that has a pointer to collection of class A members using a vector, something like this:

    vector<*A> *m_collection;

    And what I do is to create objects of class B or C and add the to the collection using push_back:

    B *myb = new B();
    m_collection->push_back(myb);

    Then I loop the collection and try to check using 'typeid' but it always return the base class (A). Is it not possible to know the exact type?

    Thank you!!!

  2. #2
    Join Date
    Dec 2007
    Posts
    401
    For typeid to be evaluated at run-time, it must be applied to a reference to a polymorphic class type.

    This means, class A must be a polymorphic type - ie. it must have at least one virtual function. for example,
    Code:
    class A { public: virtual ~A() {} /* .... */ };
    typeid must be applied to a reference (not pointer). for example,

    Code:
    A* pointer = m_collection[i] ;
    if( pointer != 0 )
    {
            A& reference = *pointer ;
            const std::type_info& typeinfo = typeid(reference) ;
    }
    This would give you the type_info corrosponding to the run-time type of the object.

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    One tint nit: your vector needs to be declared like this:
    vector<A*> m_collection;

    There's no need to use m_collection as a pointer, unless you want to pass it to a function.
    Danny Kalev

  4. #4
    Join Date
    Oct 2007
    Posts
    369
    Since you seem to be a learner (as am I), let me be so bold as to make a suggestion. Rarely should you need typeid or dynamic_cast to determine if the object is from a derived class. That is, you should rarely want to know. The base class pointer (which may point to an object of a derived class) should behave correctly without first interrogating it.

  5. #5
    Join Date
    Mar 2009
    Posts
    14
    Quote Originally Posted by hendrixj View Post
    Since you seem to be a learner (as am I), let me be so bold as to make a suggestion. Rarely should you need typeid or dynamic_cast to determine if the object is from a derived class. That is, you should rarely want to know. The base class pointer (which may point to an object of a derived class) should behave correctly without first interrogating it.
    Thanks for you advice. I am a learner too. But I don't really see the point why should I not interrogate an object which type it really is.

    Quote Originally Posted by Danny View Post
    One tint nit: your vector needs to be declared like this:
    vector<A*> m_collection;

    There's no need to use m_collection as a pointer, unless you want to pass it to a function.
    True, thanks. Yes, in the "real" code I will need to pass it to a function. I tend to use pointers all the time. I don't know if this is not a good practice. Should the code have an excess of pointers or the other way around?

    Quote Originally Posted by vijayan View Post
    For typeid to be evaluated at run-time, it must be applied to a reference to a polymorphic class type.

    This means, class A must be a polymorphic type - ie. it must have at least one virtual function. for example,
    Code:
    class A { public: virtual ~A() {} /* .... */ };
    typeid must be applied to a reference (not pointer). for example,

    Code:
    A* pointer = m_collection[i] ;
    if( pointer != 0 )
    {
            A& reference = *pointer ;
            const std::type_info& typeinfo = typeid(reference) ;
    }
    This would give you the type_info corrosponding to the run-time type of the object.
    THANK YOU!!! I worked fine!!!

  6. #6
    Join Date
    Oct 2007
    Posts
    369
    There will be rare times when you need to, which is why the capability is provided. Generally, however, a base class has virtual functions because you want it to behave polymorphically, e.g.
    Code:
    // Given that Plane and Bird are classes derived from Winged with a virtual function fly()
    Plane plane(...);
    Bird bird(...);
    Winged* winged[2] = { &plane, &bird };
    
    winged[0]->fly();
    winged[1]->fly();
    I don't need to know what kind of Winged winged[i] is, I just want it to fly like it is supposed to. If I need to know, there's a least a decent chance that I didn't need the virtual functions.

  7. #7
    Join Date
    Feb 2010
    Posts
    1
    i don't think it's a good practice to put pointers into a vector without first ensuring they are handled correctly; furthermore, using RTTI to determine an object's type IMHO sort of defeats the idea of polymorphism -- your code should be designed so it doesn't have to ask: "what type are you", the beauty of polymorphism is that the appropriate member function is called automatically and determined during runtime.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center