Talk about the replication of objects and the mutual assignment between objects in C++

  • 2020-05-05 11:29:33
  • OfStack

Copy C++ object

Sometimes need to use multiple identical objects, for example, the same type of each product from exterior to interior properties are the same, if you want to deal with each product separately, you need to build more of the same object, and for the same initialization, to define the object by previous initialization (and) more troublesome. In addition, it is sometimes necessary to preserve an object in a transient state.

C++ provides methods to clone objects to do this. This is the replication mechanism for objects.

Using an existing object to quickly copy multiple identical objects. Such as


  Box box2(box1);


Its purpose is to clone a new object box2 from an existing object box1.

Its general form is


   The name of the class   object 2( object 1);


Copy object 2 with object 1.

As you can see, it is similar to the way an object is defined, except that the arguments given in parentheses are not ordinary variables, but objects. A special constructor, the copy constructor (copy constructor), is called when the object is created. This function has the form


//The copy constructor definition.
Box::Box(const Box& b)
{
  height=b.height; width=b.width; length=b.length;
}


The copy constructor is also a constructor, but it has only one parameter, which is an object of this class (not of any other class) and takes the form of a reference to an object (the general convention plus the const declaration prevents the parameter value from being changed, lest the object value be accidentally modified when the function is called). This copy constructor assigns each member value of the argument object to the corresponding member in the new object.

Copy the statement
of the object


  Box box2(box1);


This is actually an object creation statement, creating a new object box2. Because the argument given in parentheses is an object, the compilation system calls the copy constructor (whose parameter is also an object) and does not call the other constructors. The address of the argument box1 is passed to the parameter b(b is a reference to box1), so when copying the body of the constructor, the values of each data member in the box1 object are assigned to each data member in box2.

If the user does not define the copy constructor himself, the compilation system automatically provides a default copy constructor that simply copies each data member of the class. C++ also provides another user-friendly form of replication, with assignment Numbers instead of parentheses, such as


  Box box2=box1; // with box1 Initialize the box2


Its general form is


   The name of the class   Object name 1 =  Object name 2;


Multiple objects can be copied in a single statement. Such as


  Box box2=box1,box3=box2;


Press box1 to copy box2 and box3. As you can see, this form is similar to the variable initialization statement, compare it to the statement that defines the variable:


  int a=4,b=a;


This form is intuitive and easy to use. But all it does is call the copy constructor.

Note that object replication and object assignment are conceptually and syntactically different. The assignment of an object is to an existing object, so you must define the object to be assigned before you can assign. The replication of an object is to create a new object from scratch and make it identical to an existing object (including the structure of the object and the values of its members).

You can make some changes to the main function in the example program:


int main( )
{
  Box box1(15,30,25); // define box1
  cout<<"The volume of box1 is "<<box1.volume( )<<endl;
  Box box2=box1,box3=box2; // According to the box1 To copy box2,box3
  cout<<"The volume of box2 is "<<box2.volume( )<<endl;
  cout<<"The volume of box3 is "<<box3.volume( )<<endl;
}

After line 3, the state of the three objects is identical.

Here's the difference between a normal constructor and a copy constructor.

1) formally
Class name (formal parameter list column); // declarations of common constructors, such as Box(int h,int w,int len);
Class name (class name & object name); // copy constructor declaration, such as Box(Box &b);

2) when creating an object, the argument types are different
Depending on the type of arguments, the system calls the normal constructor or the copy constructor. Such as


  Box box1(12,15,16); // The argument is an integer and the normal constructor is called 
  Box box2(box1); // Arguments are the name of the object and the copy constructor is called 

3) under what circumstances is
called A normal constructor is called when an object is created in a program. The copy constructor is called when a new object is copied from an existing object, and the object needs to be cloned in three cases:
(1) the program needs to create a new object and use another similar object to initialize it, as described above.

When the parameter of the function is the object of the class. When a function is called, the argument object needs to be completely passed to the argument, that is, a copy of the argument needs to be created. This is a copy of the argument. Such as


   The name of the class   object 2( object 1);
0

The return value of the function is an object of the class. When the return value is brought back to the function call after the function call. At this point, you need to copy a temporary object from the object in the function and pass it to the call of the function. Such as


   The name of the class   object 2( object 1);
1

The above calls to the copy constructor are automatically implemented by the compilation system, not by the user to call, as long as the reader knows that in these cases need to call the copy constructor.

C++ objects assign
to each other
if two or more objects are defined for a class, these objects of the same class can be assigned to each other, or the value of one object can be assigned to another object of the same class. The value of the object is the value of all the data members in the object.

Assignments between objects are also made using the assignment operator "=". Originally, the assignment operator "=" could only be used to assign values to a single variable, but is now extended to assign values between two objects of the same class by overloading the assignment operator.

In practice, this process is accomplished by member replication, in which the member values of one object are copied one by one to the corresponding member of another object.

The general form of object assignment is


   The name of the class   object 2( object 1);
2


Note that object name 1 and object name 2 must belong to the same class. For example


  Student stud1,stud2; // Define two objects of the same class 
  stud2=stud1; // will stud1 Assigned to stud2

Use the following example to learn how to assign an object.

The assignment of an object.


   The name of the class   object 2( object 1);
4

The result is


   The name of the class   object 2( object 1);
5

Description:
An object assigns values only to its data members, not to its member functions. Data members occupy storage space, and data members of different objects occupy different storage space. The process of assignment is to copy the state of data members of one object in storage space to the storage space of data members of another object. The member functions of different objects are the same piece of function code, and there is no need or ability to assign values to them.
You cannot include dynamically allocated data in the data members of a class, or there could be serious consequences when assigning values (just keep that in mind without going into detail here).


Related articles: