I have two base classes (base_1, base_2). They both have one pure virtual method. I have a class (class_1), which inherits them both and implements their pure virtuals. When I'm calling them from it, it's okay. When I am casting class_1 to void* and then to base_2*, it's not okay - it calls wrong method. I know, that the problem is related to multiple inheritance and offsets. But I don't know what is it, what is hapenning and why is it happening. I also would like to know, how can I achieve correct casting. Here is the code, which reproduces the problem.
class base_1
{
public:
virtual int doo_base_1() = 0;
};
class base_2
{
public:
virtual int doo_base_2() = 0;
};
class class_1 : public base_1, public base_2
{
public:
virtual int doo_base_1() override
{
return 1;
}
virtual int doo_base_2() override
{
return 2;
}
};
inline void doo()
{
class_1* ptr = new class_1;
void* void_ptr = ptr;
auto result_1 = ptr->doo_base_2(); // Correct.
auto result_2 = ((base_2*)void_ptr)->doo_base_2(); // Incorrect - it calls wrong method (in my case - doo_base_1, so the result is 1).
}