Suppose I have the following code:
class a {
public:
virtual void do_a() = 0;
}
class b {
public:
virtual void do_b() = 0;
}
class c: public a, public b {
public:
virtual void do_a() {};
virtual void do_b() {};
}
a *foo = new c();
b *bar = new c();
Will foo->do_a() and bar->do_b() work? What’s the memory layout here?
Why shouldn’t they? The memory layout will typically be something like:
If you convert your
fooandbartovoid*and display them, you’llget different addresses, but the compiler knows this, and will arrange
for the
thispointer to be correctly fixed up when calling thefunction.