I'm trying to make a function that reads a polymorphic dynamic array and prints a different thing for each class. The code is something like this:
class A {};
class B: public A {};
class C: public A {};
void function (const A &a) {
if(B *b = dynamic_cast<B*>(a)){
cout << "B" << endl;
}
if(C *c = dynamic_cast<C*>(a)){
cout << "C" << endl;
}
}
int main () {
A **array = new A* [2];
array [0] = new B;
array [1] = new C;
function (array [0]); // To print B
function (array [1]); // To print C
}
But it gives me an error that says:
cannot dynamic_cast ‘a’ (of type ‘const class A’) to type ‘class B*’ (source is not a pointer)
What can I do?