Firstly you're using typeinfo
slightly wrongly, when applied to a pointer it returns the type of the pointer, but when applied to a dereferenced pointer it returns the actual type of the object pointed to, so long as the base type has at least one virtual function ( usually it would be the destructor). So the following will work:
class Super {
public:
virtual ~Super() {}
};
class Sub1 : public Super {};
class Sub2 : public Super {};
int main() {
std::vector<Super*> vec;
vec.push_back(new Sub1);
vec.push_back(new Sub2);
assert(typeid(vec[0]) == typeid(vec[1]));
assert(typeid(*vec[0]) != typeid(*vec[1]));
}
Secondly, doing this kind of type switching is generally considered a sign that you're doing something wrong. For example instead of
void foo(Base* v) {
//Here SOMETHING and SOMETHING_ELSE are constants we calculate elsewhere.
if( typeid(*v)==SOMETHING ) { cout<<"Got a SOMETHING"<<endl; }
else if (typeid(*v)==SOMETHING_ELSE ) { cout<<"Got a SOMETHING ELSE"<<endl; }
}
or its roughly equivalent
void foo(Base* v) {
if( dynamic_cast<Something*>(v) ) { cout<<"Got a SOMETHING"<<:endl; }
else if ( dynamic_cast<SomethingElse*>(v) { cout<<"Got a SOMETHING ESLE"<<endl; }
it is usual to add the functionality into the base class:
class Base
{
public:
virtual void printMessage()=0;
};
class Something : public Base
{
void printMessage() { cout<<"Got a SOMETHING"<<endl; }
}
class SomethingElse : public Base
{
void printMessage() { cout<<"Got a SOMETHING ELSE"<<endl; }
}
void foo(Base * v)
{
v->printMessage();
}