1

Suppose I have a vector of objects of a base class, but use it to contain a number of derived classes. I want to check whether or not a member of that vector is of a specific class. How do I do that? I can think of making a template of a derived class that takes in a parameter of the base class, but I am not sure how I can compare the class with the object.

user1703993
  • 61
  • 1
  • 5
  • Here is a possible place to start: http://stackoverflow.com/questions/351845/finding-the-type-of-an-object-in-c – MikeB Oct 01 '12 at 16:57
  • 5
    If you have a vector of objects of a base class you don't have any objects of derived types; they got sliced when they were stored in the vector. – Pete Becker Oct 01 '12 at 16:59
  • 1
    The real question is why do you need to know the type at all. More often than not it is a design flaw. – Zdeslav Vojkovic Oct 01 '12 at 16:59
  • 1
    You're misunderstanding what you're doing. A vector is a homogeneous array of elements of the same type. Either you've sliced your objects into base objects, or you have a vector of pointers. In any event, your design is probably misguided. – Kerrek SB Oct 01 '12 at 17:00
  • @PeteBecker good point. I totally missed that 'detail' – Zdeslav Vojkovic Oct 01 '12 at 17:00
  • Usually when I need to store elements of different types in a vector I actually store pointers to an element of the base class in it. I assumed this was the case. – peoro Oct 01 '12 at 17:14
  • My base class vector has only derived classes. There are multiple types of derived classes, but I want to know a way in order to determine whether or not an element of that vector is of a particular type. – user1703993 Oct 01 '12 at 18:22

3 Answers3

2

If your base class has some virtual members (i.e. it's polymorphic, as I think it should be in a case like this one) you could try to down cast each member to find out its type (i.e. using dynamic_cast).

Otherwise you could use RTTI (i.e. typeid).

peoro
  • 25,562
  • 20
  • 98
  • 150
0

You can use the dynamic_cast

But if you need to do that, then you probably have a design problem. you should use polymorphism or templates to solve this problem.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
user1708860
  • 1,683
  • 13
  • 32
0

check out this example:

#include <iostream>
using namespace std;

#include <typeinfo>

class A
{
public:
    virtual ~A()
    {
    }
};

class B : public A
{
public:
    virtual ~B()
    {
    }
};

void main()
{
    A *a = new A();
    B *b = new B();

    if (typeid(a) == typeid(b))
    {
        cout << "a type equals to b type\n";
    }
    else
    {
        cout << "a type is not equals to b type\n";
    }

    if (dynamic_cast<A *>(b) != NULL)
    {
        cout << "b is instance of A\n";
    }
    else
    {
        cout << "b is not instance of A\n";
    }

    if (dynamic_cast<B *>(a) != NULL)
    {
        cout << "a is instance of B\n";
    }
    else
    {
        cout << "a is not instance of B\n";
    }

    a = new B();

    if (typeid(a) == typeid(b))
    {
        cout << "a type equals to b type\n";
    }
    else
    {
        cout << "a type is not equals to b type\n";
    }

    if (dynamic_cast<B *>(a) != NULL)
    {
        cout << "a is instance of B\n";
    }
    else
    {
        cout << "a is not instance of B\n";
    }
}
Borg8
  • 1,562
  • 11
  • 19