Does copy constructor / assignment operator of base class also gets called | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Does copy constructor / assignment operator of base class also gets called

Hi While we create derived class object, it calls constructor of base class as well. This is okay as derived class has some members which are of base and those are to be initialised. When I declare overloaded assignment operator and copy constructor in base class, I am not allowed to copy derived object in main function. I had declared those two in base class into private section and there was no similar member in derived class overloaded by us. Why those two members stop creating copy of derived object?

17th Jun 2020, 1:26 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Answers
+ 4
Supportive code : https://code.sololearn.com/c85B34dJzU75/?ref=app It compiles... Just comment out that private: line to make both copy and assignment in base class as private and it doesn't compile
17th Jun 2020, 1:36 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
derived class can not access the private members of it's base class. no type of inheritance allows access to private members. (this is what protected is for)
17th Jun 2020, 2:54 PM
MO ELomari
0
Yeah that's true but this does means it always call base copy constructor / assignment operator ? I mean it's okay for constructor or copy constructor to call base counter parts.... However, in case of assignment operator, it's already constructed and do we still need to call base counter part ?
17th Jun 2020, 3:52 PM
Ketan Lalcheta
Ketan Lalcheta - avatar