[C ++] copy constructor and assign value operator overload

Source: Internet
Author: User

Author: Feng Mingde; supplement: Shosh

Note: classes that contain dynamically assigned members should provide copy constructors and overload the "=" value assignment operator.

The following examples will be used:
Class cexample
{
Public:
Cexample () {pbuffer = NULL; nsize = 0 ;}
~ Cexample () {Delete pbuffer ;}
Void Init (int n) {pbuffer = new
Char [N]; nsize = N ;}
PRIVATE:
Char * pbuffer;
// Class objects contain pointers pointing to dynamically allocated memory resources
Int nsize;
};

The main feature of this class is to include pointers to other resources.

Pbuffer points to a memory space allocated in the heap.

I. copy constructors
Int main (INT argc,
Char * argv [])
{
Cexample theobjone;
Theobjone. init40 );

// Another object needs to be initialized and called object 1.
Cexample theobjtwo = theobjone;
...
}

Statement "cexample theobjtwo = theobjone;" use theobjone to initialize theobjtwo.

The complete method is to copy the values of all members in the memory.

After completion, theobjtwo. pbuffer = theobjone. pbuffer.

That is, they will point to the same place. Although the pointer is copied, the space to which it points is not copied, but shared by two objects. In this way, the objects are not independent, and the deletion of spaces poses a risk.

Therefore, necessary measures are required to avoid such situations.

Review the specific process of the following statement: first, create the object theobjtwo, call its constructor, and then the Member is copied.

You can add operations in the constructor to solve the problem of pointer members.

Therefore, in addition to providing default constructor, C ++ also standardizes another special constructor: copy constructor. In the preceding statement, if a copy constructor is defined in the class, when the object is created, the copy constructor is called. In the copy constructor, you can refer to the input variables, copy the resource pointed to by the pointer.

The format of the copy constructor is: constructor name (Object reference)

The cexample class provided after the copy constructor is defined:

Class cexample
{
Public:
Cexample () {pbuffer = NULL; nsize = 0 ;}
~ Cexample () {Delete pbuffer ;}
Cexample (const cexample &);
// Copy the constructor
Void Init (int n) {pbuffer = new
Char [N]; nsize = N ;}
PRIVATE:
Char * pbuffer;
// Class objects contain pointers pointing to dynamically allocated memory resources
Int nsize;
};

Cexample: cexample (const cexample & rightsides)
// Copy the definition of the constructor
{
Nsize = rightsides. nsize; // copy a regular member
Pbuffer = new
Char [nsize]; // copy the content pointed to by the pointer
Memcpy (pbuffer, rightsides. pbuffer, nsize * sizeof (char ));
}

In this way, when a new object is defined and initialized with an existing object, cexample (const cexample & rightsides) will be called, and the existing object will be passed to the constructor using the alias rightsides for copying.

In principle, a copy constructor should be provided for all classes that contain dynamically assigned members.

Another method of calling the copy constructor.

When an object is directly transmitted as a parameter to the function, the function will create a temporary copy of the object. This copy process will also be called the same copy constructor.

For example:

Bool testfunc (cexample OBJ );

Testfunc (theobjone); // The object is directly used as a parameter.

Bool testfunc (cexample OBJ)
{
// Obj-specific operations are actually performed on temporary copies after replication.
}

Another case is also related to temporary objects.

When a local object in the function is returned to the function caller, a temporary copy of the local object will also be created, and the copy constructor will also be called.
Ctest func ()
{
Ctest thetest;
Return thetest
}

Ii. overload of value assignment

The following code is similar to the previous example.
Int main (INT argc,
Char * argv [])
{
Cexample theobjone;
Theobjone. INIT (40 );

Cexample theobjthree;
Theobjthree. INIT (60 );

// Now an object Value assignment operation is required. The original content of the assigned object is cleared and filled with the content of the right object.
Theobjthree = theobjone;
Return 0;
}
"=" Is also used, but it is different from the example in "1.". In the example of "1.", "=" indicates initialization in the object declaration statement. More often, such initialization can also be represented by parentheses.

For example, cexample theobjone (theobjtwo );

In this example, "=" indicates the value assignment operation. Copy the content of theobjone to theobjthree, which involves discarding the original content of theobjthree and copying new content.

However, the default operation of "=" only copies the value of the member variable. Old values are naturally discarded.

Because the object contains pointers, it will cause adverse consequences: the pointer value is discarded, but the content pointed to by the pointer is not released. The pointer value is copied, but the content indicated by the pointer is not copied.

Therefore, in addition to providing a copy constructor, classes that contain dynamically assigned members should also consider overloading the "=" value assignment operator symbol.

Class Definition changed:

Class cexample
{
...
Cexample (const cexample &);
// Copy the constructor
Cexample & operator = (const cexample &);
// The Value assignment operator is overloaded.
...
};

// Overload the value assignment operator
Cexample & cexample: Operator = (const cexample & rightsides)
{
Nsize = rightsides. nsize; // copy a regular member
Char * temp = new
Char [nsize]; // copy the content pointed to by the pointer

Memcpy (temp, rightsides. pbuffer, nsize * sizeof (char ));

Delete [] pbuffer;
// Delete the original pointer pointing to the content (put the delete operation behind it to avoid content loss in special cases of X = x)
Pbuffer = temp; // create a new point
Return * This
}

3. Copy the code that uses the value assignment operator to overload the constructor.

Cexample: cexample (const cexample & rightsides)
{
Pbuffer = NULL;
* This = rightsides // "=" after the overload is called"
}

[Shosh]
In order to better understand the copy constructor, I also have two sentences:
1. Why is a copy constructor different from a constructor?
A: The copy constructor is actually a constructor, but its parameter is a reference of the const class's own object. If there is no pointer member in the class (the pointer member points to the dynamically applied space), there is no need to write a copy constructor. We know that if there is a class cobj, it has produced an object obja, And now it uses cobj to create objb, if the program uses the statement objb = obja; that is to say, assign values to objb directly using the obja data. This is no problem for general classes, but if cobj contains a char *
Pstr Member, used to store the address of the dynamically applied string. In obja, the new method is used to dynamically apply for the memory and make the obja. pstr points to the requested space, after ojbb = ojba, obja. pstr and objb. pstr points to the space at the same time, so that no one knows who is responsible for releasing the space, it is likely that the same memory is released twice. Apply for obja first by using the copy constructor. pstr points to the space of the space size, and then copies the space content, so that it does not point to the same memory. Each has its own applied memory, and is responsible for releasing its own applied memory, this solved the problem just now. Therefore, the "copy" here copies the content of the dynamically applied space, rather than the data of the class itself. Note that the parameters of the copy constructor are object references rather than object pointers. As for why the reference should be used, it cannot be understood by the pointer for the moment.
2. Why do I need to overload the = value assignment operator?
A: In the following example, when you use the statement objb = obja, The pstr of objb may already point to the dynamically applied space. If you simply overwrite the address that it points, this will cause memory leakage. Therefore, you need to reload the = value assignment operator. In the overload function, determine that if pstr has pointed to the dynamically applied space, release it first.
3. Copy the constructor and the = value assignment operator overload relationship.
A: From the original example, we can see that the = assignment operator overload is much more than the copy constructor, except for copying the memory data dynamically applied by the constructor, the memory space originally applied for is also released. Therefore, the implementation of the copy constructor provided at the end of the original text can be completed using the overload of the = value assignment operator.
4. When will the copy constructor be called?
A. the object's direct value assignment will also call the copy constructor (since the = value assignment operator is overloaded, why do I need to call the copy constructor ?);
B. If the function parameter is passed by value, the copy constructor is also called;
C. If the function returns a value, the copy constructor is also called.
5. As in question 4, since the = value assignment operator is overloaded, why do I need to call the copy constructor?
A: Write a small program to test it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.