0

About Copy Constructor

Hi, currently learning and practicing constructors and operator overloading subjects. Have some questions if anyone wants to answer. First one is about the copy constructor... How can it work without overloading the equals (=) operator? It's working just like T1(T2), but I couldn't understand the way it works. Second, the copy constructor accesses the second class' private variable. How can this be done? https://code.sololearn.com/cLX3GB4S7z6p/?ref=app https://code.sololearn.com/cLX3GB4S7z6p/?ref=app

16th Mar 2021, 11:26 AM
ogoxu
ogoxu - avatar
2 Respostas
+ 4
As long as it is used in an initialization, the = is not the assignment operator, but just syntax for copy initialization, which calls the copy constructor. The assignment operator is not used for constructing objects, but for (possibly) changing existing objects. https://en.cppreference.com/w/cpp/language/copy_initialization The access specifiers only define the access to members of the class from the outside. Internally, all members have access to all other members the class has access to (all variables, methods, constructors, ..., have access to all other variables, methods, constructors, ...). https://en.cppreference.com/w/cpp/language/access
16th Mar 2021, 12:31 PM
Shadow
Shadow - avatar
+ 1
Shadow thank you
16th Mar 2021, 1:42 PM
ogoxu
ogoxu - avatar