What is a copy constructor?
Only a single parameter, which is a reference to objects of this class type (commonly used const adornments), is called a copy constructor.
When do I use the copy constructor?
1. Displays or implicitly initializes an object based on another object of the same type.
2. Copy an object and pass it as an argument to a function.
3. Copy an object when the function returns.
4. Initialize the elements in the sequential container.
5. Initialize the array elements according to the element initialization list.
1) The form of the object definition
C + + supports two types of initialization: direct initialization and replication initialization. Copy initialization uses the = symbol, and direct initialization places the initialization in parentheses.
When used with class-type objects, the replication form and the direct form of initialization are different: direct initialization calls directly to the constructor that matches the argument, and replication initialization always calls the copy constructor. Copy initialization first creates a temporary object with the specified constructor, and then copies that temporary object to the object being created with the copy constructor.
For example:
String null_book = "9-999-99999-9"; Replication initialization
String dots (10, '. '); Direct initialization
String empty_copy () = string (); Replication initialization
String Empty_direct; Direct initialization
2) shape participation return value
When a parameter is a non-reference type, the value of the argument is copied. When a non-reference type is returned as a return value, a copy of the value in the return statement is returned.
3) Initialize the container element
The copy constructor can be used to initialize the elements in the sequential container. For example
Vector<string> Svec (15);
The compiler first uses the string default constructor to create a temporary value to initialize the Svec, and then uses the copy constructor to copy the temporary values to each element of the Svec.
4) constructors and array elements
If you provide initialization of the display element by using an array initialization list of regular curly braces, the copy initialization is used to initialize each element. Creates an element of the appropriate type from the specified value, and then uses the copy constructor to copy the value to the appropriate element.
Synthetic copy Constructors
If we do not define a copy constructor, the compiler will synthesize one for us. Unlike the composition's default constructor, a copy constructor is synthesized even if we define other constructors. The behavior of the composite copy constructor is to perform a member-by-instance initialization to initialize the new object to a copy of the original object.
For example:
Class Example
{
Private
int num;
std::string str;
}
The composition copy constructor is as follows:
Example:: Example (const example& Other): num (other.num), str (OTHER.STR) {}
define your own copy constructor
For many classes, the synthetic copy constructor accomplishes only the necessary work. A class that contains only members of a class type or a built-in type (but not a pointer type), and you can copy it without having to define the copy constructor in the display.
However, some classes must control what happens when objects are copied. Such a class often has a data member that is a pointer, or has a member that represents another resource that is assigned in the constructor. Other classes must do some specific work when creating new objects . In both cases, the copy constructor must be defined.
Prohibit copying
Some classes require a complete ban on replication. For example, the Iostream class would not allow replication. To prevent replication, we must define a copy constructor ourselves and declare it as private, and do not define it.
Because if we do not declare a copy constructor, the compiler synthesizes one, declares the copy constructor as private and does not define whether to prevent friends and members from replicating. By declaring (but not defining) the private copy constructor, you can suppress any attempt to replicate a class-type object.
It is also important to note that if you define a copy constructor, you must define a default constructor, because once we have defined the constructor the compiler will not synthesize the default constructor.
Finally, use the two textbook examples to consolidate.
1. Write a copy constructor for the following class to copy all the members. Copy the object that pstring points to instead of copying the pointer.
struct Noname { Noname (): pstring (New std:: string), I (0), D (0) {} private: std::string *pstring; int i; Double D;};
For:
Noname::noname (Const noname& PG) { pstring = new std::string; *pstring = * (pg.pstring); I= pg.i; D= PG.D;}
2. Which class might need to define a copy constructor?
(a) A point3w class containing four float members.
(b) Matrix class, where the actual matrix is dynamically allocated in the constructor and deleted in the destructor.
(c) The Payroll class, which provides a unique ID for each object in this class.
(d) A word class that contains a string and a vector with a column position as an element.
A: (a) is not required because the data members in this class are built-in types, without pointer members, using the compiler-provided composition copy constructor.
(b) need, involving dynamic allocation of pointers and memory.
(c) need to provide a unique ID when creating a copy of a payroll object that already exists.
(d) Not required, the compiler automatically invokes the copy constructor of string and vector for its data members.
Copy constructors for C + + replication control