#include <iostream>
usingnamespace std;
class Base
{
public:
virtualbool foo(Base *) = 0;
};
class A : public Base
{
public:
bool foo(Base *b)
{
B* b1 = dynamic_cast<B*>(b);
return b1 == 0;
}
};
class B : public Base
{
public:
bool foo(Base *b)
{
A* a = dynamic_cast<A*>(b);
return a == 0;
}
};
int main()
{
Base *aBase = new A();
Base *bBase = new B();
A *a = new A();
B *b = new B();
cout << boolalpha;
cout << aBase->foo(bBase) << endl;
cout << aBase->foo(a) << endl;
cout << a->foo(bBase) << endl;
cout << a->foo(b) << endl;
system("pause");
}
errors
'B' : undeclared identifier: line 14
'b1': undeclared identifier: line 14
syntax error: identifier 'B': line 14
'b1': undeclared identifier: line 15
i think is because i use 'class B' before its definition..
In that respect, if i add a forward declaration to 'B' in line 3, i get B*: invalid target for dynamic_cast