0

I have something like this:

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

class B : public A
{
 //stuff
};

class C : public A
{
 //stuff
};

class D : public A
{
 //stuff
};

void Collision()
{
 //obj is a multidimensional array of class A that stores objects from class B, C and D
 if(obj[x][y] != B && obj[x][y] != C) //type name is not allowed
    doStuff();
}

I'm getting the error: type name is not allowed

I know it is not suposed to compare the objects like this, but i dont know how i should do it.

xRed
  • 1,895
  • 6
  • 20
  • 36
  • If `obj` is an array of polymorphic objects rather than pointers to polymorphic objects you're already in for a world of hurt. – Captain Obvlious Dec 26 '13 at 01:55
  • obj is defined like this: A ***obj – xRed Dec 26 '13 at 01:56
  • 1
    This may be what you are looking for http://stackoverflow.com/questions/351845/finding-the-type-of-an-object-in-c – Jesse Laning Dec 26 '13 at 01:57
  • Then you're sort of ok. I recommend using `std::vector>>>`. Replace `std::vector` with `std::array` if the size of your arrays are fixed. Replace `std::unique_ptr` with `std::shared_ptr` or `std::weak_ptr` as appropriate. – Captain Obvlious Dec 26 '13 at 01:58
  • Could you elaborate on what exactly do you mean with _'compare'_? Do you want to know if an object is of a specific type, or do you want to compare two objects of different kind? – πάντα ῥεῖ Dec 26 '13 at 02:02
  • I want to know if the object obj is a object of type B or C, in this particular case i want to know if they're different from B anc C, meaning i just want objects from type D – xRed Dec 26 '13 at 02:10

1 Answers1

3
#include <typeinfo>

void Collision()
{
    if (typeid(obj[x][y]) != typeid(B) && typeid(obj[x][y]) != typeid(C)) 
        doStuff();
}
Mark H
  • 13,797
  • 4
  • 31
  • 45