Question:
Copy constructor in C++?
Himanshu Gajwani
2012-06-19 02:15:45 UTC
Ok I have read number of books and did google It and all but still It didn't click me. I still don't have clarity on the topic. Why will you use copy constructor, when will you use, how to use (syntax)?

PS : I am a student learning c++.
Four answers:
Paul
2012-06-19 02:41:27 UTC
Well whenever you create a custom class it has your default constructor which is called when the variable is simply declared.



However you can overload this constructor with additional variables.



For example let's say we have a class called wallet and we want it so when we create a value the default amount of money is 0. However we want their to also be an option so that we can declare it with a specified amount of money.



so mywallet = Wallet. //the total is 0

or mywallet = Wallet(100) //the total becomes 100



this is define in your constructor which normally follows what you define in your header or cpp file.

Wallet (); // Use default constructor (Note there is no return value)

Wallet (int total); // Use cosntructor with int.



This concept can further be applied to use a constructor with the same variable.

Wallet(Wallet); //this is the prototype for the copy constructor.



The point of a copy constructor is important as normally if you write.

Wallet myWallet = susansWallet it will be a shadow copy meaning myWallet now points to



SusansWallet if there is no copy constructor defined. So if you make any changed to Susans wallet it will be changed on myWallet and vice versa.



Now with the copy constructor you can specify what happens when you use wallet mywallet =susanswallet.



so for example:



Wallet(Wallet test){

total = test.total;

owner = test.owner;

}



so now myWallet and susansWallet will be equal to each other but not actually point to each other so changes made to myWallet will not affect susansWallet.
godfatherofsoul
2012-06-19 02:31:04 UTC
The copy constructor is implicitly called whenever you assign one class variable to another:



MyClass a;

MyCalss b=a;//copy constructor called under the hood



If you don't create a copy constructor, I believe C++ just copies the values in the members from one to the other. C++ can be a royal pain when it comes to dealing with constructors. I suggest looking up the High Integrity C++ Coding Standard for some explanations of the hazards of using constructors.
?
2016-07-19 13:10:21 UTC
First, the normal constructor is used whilst you create an object yourself. For illustration: type MyClass MyClass (int v1, int v2); ... ; MyClass mc(1,2) in this example you explicitly create the variable mc. The reproduction constructor is used when you wish to have to make a copy of one more object. This is often used when passing parameters to features. Whilst you declare a operate akin to: int func(MyClass m); The perform recieves a replica of the parameter. This reproduction is constructed utilizing the replica constructor. You could also use the reproduction constructor yourself if you happen to come to a decision to create a object copied from another. Category MyClass MyClass(int v1, v2); // commonplace constructor MyClass(MyClass const &different); // reproduction constructor ... ; MyClass::MyClass(MyClass const &different) //reproduction values from different v1 = other.V1; // and so on. Void f1(MyClass m); // This func takes a duplicate of MyClass MyClass mc(1,2); // Create MyClass object with constructor MyClass mc2(mc1) // mc2 is a copy of mc f1(mc); // WHen f1 is known as, it receives a duplicate of mc. F1 can do something it wants with it can be reproduction of mc. With it can be own replica, some thing it does does no longer affect the long-established.
Rexxar
2012-06-19 02:18:34 UTC
Here is an example:

class MyClass {

...

};



MyClass var1; // Use default constructor

MyClass var2(var1); // Use copy constructor


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...