Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was learning about copy constructors in c++, and it stated that if you dont create your own copy constructors in c++, the compiler will for you. And my question was:
1. why would you need to make your own copy constructor when the compiler does it for you?

What I have tried:

i have tried searching it up but find no results
Posted
Updated 9-Jul-21 2:28am

Because the default copy constructor creates a copy of that object only: it copies any pointers that class contains instead of creating new memory and copying the content of teh pointer into that.

So assume you have a class called Learning that contains a two fields: a pointer to a Student, and a pointer to a Subject.
If you create a Learning object and assign it to a Student and a Subject ("Joe Smith", "Maths") stored in a Learning type variable called JoeSmith, then copy it into a new Learning type variable called "MikeHunt" using the default copy constructor then the two variable point at different Learning objects - which is what you want. However, because the default copy constructor only copied the pointers any changes you make to the Student or Subject objects from JoeSmith will also affect the Student and / or Subject that MikeHunt is referring to, and vice versa.

That isn't what you wanted, not at all: if Joe Smith pays his tuition fees and you record the payment as part of his Student information, you definitely don't want it affecting Mike Hunt's payments!

So you create a Learning copy constructor which creates new Student and Subject objects and copies the data into the new ones. Not Jow and Mike are independant and don't affect each other.

Make sense?
 
Share this answer
 
Comments
iwanttoaskquestions 9-Jul-21 8:26am    
oh ok, thank you i understand now
OriginalGriff 9-Jul-21 8:38am    
You're welcome!
CHill60 9-Jul-21 9:07am    
5'd for the choice of variable names :laugh:
OriginalGriff 9-Jul-21 9:56am    
I don't know what you mean ... :InnocentWhistle:
If the original owns something, the question is whether the copy can share ownership of that thing. In many cases, the item in question will have been allocated from the heap. If ownership will be shared, who will be responsible for deleting the shared item? There's shared_ptr to take care of that now, but perhaps the item represents a real-word resource that cannot be shared, in which case the copy constructor must allocate a new instance of that item. The compiler won't always get it right, so the flexibility of a copy constructor is required.

The Rule of 3[^] says that if you have a destructor, you typically need a copy constructor and copy operator (operator=). For efficiency, C++11 added the move constructor and move operator, so now the Rule of 3 is often the Rule of 5.

If you do a web search on "C++" plus "copy constructor", "copy assignment operator", "move constructor", or "move assignment operator", you will find lots of things to read.
 
Share this answer
 
v2
Comments
iwanttoaskquestions 9-Jul-21 8:26am    
alright, thank you
The default copy constructor is a shallow copy. That is it is a straight memory copy of the object. However, if any of the properties in the object are pointers to other objects, or memory, only the pointers are copied. So if any of that data changes at a later time, the copied object may be corrupt.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900