So this is actually a somewhat contrived case of runtime duck typing in C++? Class A and class B may not actually be related to each other but so long as they have a compatible interface of virtual methods, the cast will work?
EDIT: The following seems to compile, even without there being any inheritance at play at all.
Code:
class BaseClassA
{
virtual void test() {}
};
class BaseClassB
{
virtual void test() {}
};
int main()
{
BaseClassA* const a = new BaseClassA;
BaseClassB* const b = dynamic_cast < BaseClassB* > ( a ); // Does not work
delete a;
return 0;
}
EDIT #2: Oh well, on closer inspection, the cast actually fails.
Code:
#include <iostream>
class BaseClassA
{
public:
virtual void test() const
{
std::cout << "BaseClassA::test\n";
}
};
class BaseClassB
{
public:
virtual void test() const
{
std::cout << "BaseClassB::test\n";
}
};
int main()
{
const BaseClassA* const a = new BaseClassA;
const BaseClassB* const b = dynamic_cast < const BaseClassB* > ( a );
if ( ! b )
{
std::cerr << "Cast failed!\n";
return 1;
}
b->test();
delete a;
return 0;
}