placid said:
is there another way then a dynamic_cast ?
Hi,
Unlike Java, C++ does not have a common polymorphic ancestor. This does
not stop you from doing the following, what may be more or less what
java does behind the scenes, I guess:
#include <typeinfo>
#include <iostream>
struct Base
{
bool instanceOf( Base const& other ) const
{
//Just doing this for clarity...
const std::type_info& myInfo( typeid(*this) );
const std::type_info& otherInfo( typeid(other) );
//Could have used: return( typeid(*this)==typeid(other) );
return ( myInfo == otherInfo );
}
virtual ~Base(){}
};
struct Derived : Base
{
};
int _tmain(int argc, _TCHAR* argv[])
{
Base base;
Derived d1;
Derived d2;
std::cout << "D1, D2:" << d1.instanceOf( d2 ) << std::endl;
std::cout << "D1, Base: " << base.instanceOf( d1 ) << std::endl;
std::cout << "Base, D1: " << d1.instanceOf( base ) << std::endl;
std::cin.get();
}
//Output:
D1, D2:1
D1, Base: 0
Base, D1: 0
You go figure!
Regards,
Werner