How does object slicing work in c++?
Could somebody please help me understand how object slicing works under the hood. To be specific, I have class Class3 which is inheriting classes Class1 and Class2 publicly like below
class Class1
{
int a, b;
public:
Class1() : a(11), b(12){}
void get(){cout<<"\nget from Class1: this:"<<this;}
};
class Class2
{
int c, d;
public:
Class2() : c(13), d(14){}
void get(){cout<<"\nget from Class2: this:"<<this;}
};
class Class3 : public Class1, public Class2
{
public:
Class3(): Class1(), Class2(){};
void get(){cout<<"\nget from Class3: this:"<<this;}
};
int main()
{
Class3 obj;
Class1& c1ref = obj;
Class2& c2ref = obj;
c1ref.get();
c2ref.get();
return 0;
}
Output:
get from Class1: this:0x70d12c86dd30
get from Class2: this:0x70d12c86dd38
My understanding is when we assign c1ref/c2ref with obj, it must be doing some calculations internally before it assigning value to c1ref/c2ref. How are these values calculated?