Calling virtual function of class which derives from two base classes crashes with hardfault
I have found a problem when deriving a class from two base classes.
class BaseA
{
public:
BaseA() {}
virtual int GetNumber() const {return 42;}
};
class BaseB
{
public:
BaseB() {}
virtual int32_t MathConfuscator(int32_t input) {return input * 97 % 5;}
};
class DerivedC : public BaseA, public BaseB
{
public:
DerivedC() {}
int GetNumber() const override {return 10;}
int32_t MathConfuscator(int32_t input) override {return input * 10;}
};
Running these classes in a program e.g.:
int main()
{
DerivedC myDerive;
int64_t myTick = 4078;
int32_t tmp = ((BaseA*)&myDerive)->GetNumber();
if(myTick == ((BaseB*)&myDerive)->MathConfuscator(tmp))
return 1;
}
The first function call GetNumber() is executed correctly and jumps into the DerivedC function while the MathConfuscator function call crashes with a hard fault.
I have tested this on an STM32G031 Nucleo-32 evaluation board using the STM32G031K8T6 procesor. The compiler chain is "9 2020-q2-update".
To me it is unclear why I do get a hard fault when running this code. If the code would be wrong I would expect a compiler error or a linker problem.
Can you give me a hint what I am doing wrong or where I might look for an answer to this problem?
Thanks,
Rasmus Kölln
