I haven't really been following this thread... so forgive me if I repeat what has already been said. But there are a few things here that stand out to me that need to be addressed.
The biggest of which is that what you are trying to do is "wrong". Or at least, it's very likely you are using your classes incorrectly.
The whole point of having an abstract base class (in this case, Animal) is so that you can treat
ALL animals the same. Checking to see whether or not this particular Animal is a Dog is pointless. With a proper design, your code should not care what kind of animal it is dealing with. It should be able to work with any of them.
That said... there are certainly occasions where you'll need to do this... but they are very rare... and I'm all but certain you do not need to do them here.
The common alternative is to use virtual functions to differentiate which type you have. IE:
1 2 3 4 5 6 7 8 9 10 11 12
|
class Animal
{
//...
virtual void announce() = 0;
};
//...
class Dog : public Animal
{
//...
virtual void announce() { cout << "I'm a dog."; }
};
|
Since the announce function is virtual, when you call it, it will automatically (and very quickly) figure out what type of animal it is, and call that animal's specific announce function. So Dogs, Cats, Birds, etc can all announce differently... but code which
uses them can just call a generic announce function without having to care what specific kind of Animal they have.
The general rule here is... if you have to downcast... you're doing it wrong.
EDIT:
Another thing is this:
1 2 3 4 5 6
|
void enqueue(Animal* a)
{
a->setOrder(order);
order++;
if(typeid(a)==typeid(Dog)) cout<<"It's a Dog<<endl; // <- wrong
}
|
'a' is of type
Animal*
because that's how it was defined. It will never be of type
Dog
so that if block will never execute.
*a
could be of type
Animal
or of any derived type. So the correct comparison here would be:
if( typeid(*a) == typeid(Dog) )
But again.. you should not be using typeid this way anyway. This is the wrong way to approach this problem.