Regarding when call copy constructor of base class, we have the form:
DerivedClass ( const DerivedClass& obj ) : BaseClass ( obj )
{
// ...
}
I just do not understand why we pass object of derived class to the base class copy constructor.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What else would you want to pass? Because the only other option is a magical pony conjured up from the air.
It’s a typical “real type hidden behind a reference to parent” thing. Base class’ copy ctor has signature
base(const base&). It doesn’t care if you pass a most derived type, or semi derived type, or maybe base type. It only sees the portion it cares about, which isbase, and nothing else, so it can do its job properly.Of course that only works for types derived using public inheritance, which forms is-a relationship between the two.