#include <iostream>
using namespace std;
struct Left
{
   char i = 'k';
};
struct Right
{
   int a = 99;
};
class Bottom : public Left, public Right
{};
int main()
{
    Bottom b;
    Left l = b;
    cout << l.i;
    Right r = b;
    cout << r.a;
    return 0;    
}
// output
// k99
How did this work?
if the memory layout of Bottom is:
Left
Right
Bottom
Then slicing b (i.e. Bottom) to Left object, should be ok, but how can it work when I slice Bottom to Right object?
Note: all this would be ok if I used casting. But I did not.
Rightpart, what is the problem? In other words you calledRight::Right(Right const &)withbas argument and that function can readb.aand assignthis->aRight r = bI obtain aRightobject from aBottomobject, without casting. so how did the compiler figure out the correct offset?Bottom, it knows where the parts are in just the same way it knows where any particular member variable isRight r = bshould end up invoking a conversion assignmentRight& operator=(const Bottom&)