Base pointer offset adjustment for multiple inheritance

Question | Nov 25, 2015 | nextptr 

Multiple inheritance has always been a thorny subject for the complexities it introduces to the application and language design. Its opponents have a common argument that multiple-inheritance violates the Single Responsibility Principle. Nevertheless, the multiple-inheritance is a powerful tool available to C++ programmers that can help solve many peculiar design problems. Here we will discuss one particular pain that a C++ compiler has to go through when objects are accessed polymorphically.
The following code has 2 base classes namely B1, B2 and a derived class C. To keep our example simple we are not using any virtual methods here.

class B1 {
    int x;

class B2 {
    int y;

class C : public B1, public B2 {
    int z;

C* pC = new C;
B1* pB1 = pC; // No offset adjustment
B2* pB2 = pC; // Offset adjustment

The following figure illustrates how the instances of B1, B2, and C would be laid out in memory:

enter image description here

Note that the order in which base types are specified in an inheritance clause decides the order in which their data would be laid out in the derived object.
As shown above, accessing derived object - *pC - through first base-class pointer, pB1, involves no extra effort from compiler's side; but access through pB2 involves under the hood offset adjustment of base address by the size of first base-class.

To help you understand this concept try answering this question:

printf("%s\n",(void*)pB1 == (void*)pB2 ? "Yes" : "No");  

printf("%s\n",(void*)(pB1 + 1) == (void*)pB2 ? "Yes" : "No");

What would be the output of the above?