Suppose I have two classes A, and B. B is derived from A. A has no data members, however B has two integer members.
If I define a method in class A, like the following:
void CopyFrom( const A* other )
{
*this = *other;
}
And call it in the child class, will the integer data member get copied?
No. This is known as the slicing problem.
This is true even if you overload operator=
in both A
and B
: *this = *other
will only ever resolve to A::operator=(const A&)
or B::operator=(const A&)
being called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With