I know base offset adjustment will happen in this situation
class Mother {
public:
virtual void MotherMethod() {}
int mother_data;
};
class Father {
public:
virtual void FatherMethod() {}
int father_data;
};
class Child : public Mother, public Father {
public:
virtual void ChildMethod() {}
int child_data;
};
Father *f = new Child;
During compilation, this code is equivalent to
Child *tmp = new Child;
Father *f = tmp ? tmp sizeof(Mother) : 0;
My question is how this offset is determined in the compilation phase? for example, in the following case
void fun(Father* f) {
// do something
}
we don't know what object the pointer will receive, how is it possible to determine whether or how much offset adjustment is needed during compilation.
I hope someone can answer my doubts, Thank you very much!
CodePudding user response:
The caller of a function knows exactly what types are involved and does the necessary adjustments.
That is,
Child c;
fun(&c);
behaves exactly the same as
Child c;
fun(static_cast<Father*>(&c));
but the conversion is implicit.