#include <iostream>
#include <typeinfo>
struct A
{
virtual ~A() {}
};
struct B : A
{
};
struct C : A
{
};
C *c = new C;
B *b = new B;
A *a = new A;
void foo (A *a)
{
if(dynamic_cast<B*>(a))
{
std::cout << "Type is B*" << std::endl;
//cannot dynamic_cast 'a' (of type 'struct A*') to type 'struct B*' (source type is not polymorphic)
}
if(dynamic_cast<C*>(a))
{
std::cout << "Type is C*" << std::endl;
//cannot dynamic_cast 'a' (of type 'struct A*') to type 'struct C*' (source type is not polymorphic)
}
}
int main()
{
foo(b);
foo(c);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dHlwZWluZm8+CgpzdHJ1Y3QgQQp7Cgl2aXJ0dWFsIH5BKCkge30KfTsKCnN0cnVjdCBCIDogQQp7CgkKfTsKCnN0cnVjdCBDIDogQQp7CgkKfTsKCkMgKmMgPSBuZXcgQzsKCkIgKmIgPSBuZXcgQjsKCkEgKmEgPSBuZXcgQTsKCnZvaWQgZm9vIChBICphKQp7CiAgICBpZihkeW5hbWljX2Nhc3Q8Qio+KGEpKQogICAgewogICAgICAgIHN0ZDo6Y291dCA8PCAiVHlwZSBpcyBCKiIgPDwgc3RkOjplbmRsOyAKICAgIC8vY2Fubm90IGR5bmFtaWNfY2FzdCAnYScgKG9mIHR5cGUgJ3N0cnVjdCBBKicpIHRvIHR5cGUgJ3N0cnVjdCBCKicgKHNvdXJjZSB0eXBlIGlzIG5vdCBwb2x5bW9ycGhpYykKICAgIH0KCiAgICBpZihkeW5hbWljX2Nhc3Q8Qyo+KGEpKQogICAgewogICAgICAgIHN0ZDo6Y291dCA8PCAiVHlwZSBpcyBDKiIgPDwgc3RkOjplbmRsOyAKICAgICAgICAvL2Nhbm5vdCBkeW5hbWljX2Nhc3QgJ2EnIChvZiB0eXBlICdzdHJ1Y3QgQSonKSB0byB0eXBlICdzdHJ1Y3QgQyonIChzb3VyY2UgdHlwZSBpcyBub3QgcG9seW1vcnBoaWMpCiAgICB9Cn0KCmludCBtYWluKCkKewoJZm9vKGIpOwoJZm9vKGMpOwoJcmV0dXJuIDA7Cn0=