Does copy constructor / assignment operator of base class also gets called | SoloLearn: Learn to code for FREE!

+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?

6/17/2020 1:26:37 PM

Ketan Lalcheta

3 Answers

New Answer

+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

+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)

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 ?