Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please,help me and if someone know answer,please explain in clear and easy way :)
Thanks in advance :)
What is copy constructor?why do we use it?what is its difference from standart constructor?

What I have tried:

Actually i read about it,but i can't understand it.What is copy constructor?why do we use it?why must we use it?
but also i can't find clear information about it :) Many of articles about it are written in difficult way.
Posted
Updated 28-Aug-16 13:22pm
v3

1 solution

The simplest is to find out what is copy contrsuctor in c++ is to read documentation.

Quote:
A copy constructor of class T is a non-template constructor whose first parameter is T&, const T&, volatile T&, or const volatile T&, and either there are no other parameters, or the rest of the parameters all have default values.

Source: Copy constructors - cppreference.com[^]

More at:
C++ Copy Constructor[^]
Copy Constructors and Copy Assignment Operators (C++)[^]
 
Share this answer
 
Comments
Member 12702056 28-Aug-16 19:09pm    
Thank you :),but actually i read about it as i wrote.
However why must we copy an object ?why is this important?
I mean if it would not be important,compiler should not call it automatically.This mean it is important for program.But why?i read ,but still i can't find answer for my this question
[no name] 28-Aug-16 19:25pm    
Begin by learning how to use Google properly, seriously : http://stackoverflow.com/questions/3278625/when-do-we-have-to-use-copy-constructors
Member 12702056 29-Aug-16 6:52am    
Thanks :)
Philippe Mori 28-Aug-16 20:32pm    
Read documentation at each links carefully and you should understand copy-construction.

Essentially a copy constructor is code that is executed when you make a copy of an existing object. They are used because they are useful to write efficient program.

If you don't care much about performance, you can use higher levels language like C# or Java for example that have garbage collector and you won't have to deal as much with low level details.

On the other side, you have C or even assembler where you need to take care of all details yourself.
Member 12702056 29-Aug-16 6:52am    
Thank you much :)

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