Question:
C++ Copy Constructors Help.?
2009-02-11 09:07:52 UTC
I'm working on some C++ programming for school. I'm just a little confused as to the purpose of the copy constructors. I understand that regular constructors within classes create a class with the defined values in the constructor, but I don't see the use of the copy one. Any help would be greatly appreciated.
Three answers:
Nobody
2009-02-11 09:56:21 UTC
Defualt Constructor: Constructor without any additional parameters



Constructor: method that gets called when someone initializes an object, which sets important starting values for the "new" object



Copy Constructor: Method that you create that allows you to copy the contents of one object into another.



Create two new objects and try and do this within your source code



object2 = object1;



It doesn't work, why? Because the assignment operator wasn't written to work with your class variables, so to actually load the contents of one object into another you'd either have to do some type of itterative loop to set all the attributes (data members) of one function to another when you are trying to create a copy of the data. You could either do this every single time, or create a copy constructor to do it for you, and overload an operator to give you the ability to use a standard operator TO CALL the copy constructor (which references the newer object), so that object2 = object1, will actually work and create an exact copy of object1.
alene
2016-05-24 09:20:16 UTC
First, the regular constructor is used when you create an object yourself. For example: class MyClass { MyClass (int v1, int v2); ... }; MyClass mc(1,2) In this example you explicitly create the variable mc. The copy constructor is used when you want to make a copy of another object. This is commonly used when passing parameters to functions. When you declare a function such as: int func(MyClass m); The function recieves a copy of the parameter. This copy is constructed using the copy constructor. You can also use the copy constructor yourself if you decide to create a object copied from another. class MyClass { MyClass(int v1, v2); // regular constructor MyClass(MyClass const &other); // copy constructor ... }; MyClass::MyClass(MyClass const &other) { //copy values from other v1 = other.v1; // etc. } void f1(MyClass m); // This func takes a copy 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 called, it receives a copy of mc. f1 can do whatever it wants with it's copy of mc. With it's own copy, whatever it does does not affect the original.
Bruce2008
2009-02-11 09:31:59 UTC
Ugh... copy constructors! And having to manage objects and supply references to subroutines! I know it won't help, but these are among the reasons I no longer deal with C++ and use Java. C++ was a real nice extension to C, but it would have been better to just start over.


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