I came across an instance the other day when I had a function, pointing to the base type, which I then needed to add to the derived type to access some additional functions. However, dynamic_castit failed, which was odd because my type definitely inherited the base type.
To understand what was going on, I created the following test program, which, I think, repeats what I saw:
void cast(TestClass *baseType)
{
if (dynamic_cast<Derived *>(baseType))
TRACE("cast was sucessful");
else
TRACE("cast failed");
}
int main(int argc, char *argv[])
{
Derived *test1 = new Derived();
TestClass *test2 = new TestClass();
TestClass test3;
test1->identify();
test2->identify();
cast(test1);
cast(test2);
test2 = test1;
test2->identify();
cast(test2);
test3 = *test1;
test3.identify();
cast(&test3);
return a.exec();
}
, , cast(), , . , ; , . , , , , . , static_cast, ?