uss
uss

Reputation: 1309

how object slicing happens in c++?

both the lines 1 and 2 will do object slicing or line one alone? thanks for your time !

class Base{
};

class Derived : public Base{
};

main()
{       
   Derived d1;
   Base b1 = d1;  //line 1
   Base &b2 = d1; // line 2
}

Upvotes: 1

Views: 101

Answers (1)

David Heffernan
David Heffernan

Reputation: 613063

Only the first assignment can lead to slicing, since that assignment involves a copy. The second assignment merely takes a reference to the object. Without a copy, there can be no slicing.

Upvotes: 3

Related Questions