I have polymorphic classes derived from a base class A:
class A {
public:
virtual void fV() { }
};
class B : public A {
public:
void mB() { }
};
class C : public A {
public:
void mC() { }
};
I want now to iterate over an array pointing to such objects and check the type equivalence:
A *array[4] = { new B, new C, new C, new B };
for(int i = 0; i < 4; ++i) {
cout << i + 1 << ". " ;
(typeid(array[i]) == typeid(A)) ? cout << 1 << ' ': cout << 0 << ' ';
(typeid(*array[i]) == typeid(B)) ? cout << 1 << ' ': cout << 0 << ' ';
(typeid(*array[i]) == typeid(C)) ? cout << 1 << ' ': cout << 0 << ' ';
cout << endl;
}
The result is:
1. 0 1 0
2. 0 0 1
3. 0 0 1
4. 0 1 0
I expect a type equivalence in the first condition, but as result I obtain a failed comparison (0 0 0 0 in the first column). The results of second and third condition are as I expected.
What is wrong with the first condition?