STL programming practice three: analysis of the copy construction and assignment characteristics of the STL container

zhaozj2021-02-11  172

STL programming practice three: analysis of the copy construction and assignment characteristics of the STL container

Copy construct

Both STL containers support the insertion of elements, but when you insert a custom class object you have to know how the STL container completes the insert, what is the basic requirement for the inserted custom class object. Otherwise, you will have an error you can't think of. for example.

Class testcpyconstruct

{

PUBLIC:

Testcpyconstruct (): data (null)

{

Data = (char *) Operator New (size);

MEMSET (DATA, 0, SIZE);

}

~ Testcpyconstruct ()

{

Operator delete (data);

}

Private:

ENUM {SIZE = 128};

CHAR * DATA;

}

The above TestCPyConstruct class is defined. Now we write a piece of code that defines a vector to store the TestcPyConstruct object.

void main ()

{

Vector Vect;

Testcpyconstruct Obj;

Vect.push_back (obj);

}

When you compile and run, it will be wrong because illegal access. Therefore, we can definitely use the following pseudo code in the VECTOR container of STL.

NEW (& Memaddr) TestcPyconstruct (Const TestcpyConstruct &);

Where MemadDR is a memory address dynamically assigned by the container, which calls the copy constructor of TestcPyConstruct. Since we did not define a copy constructor in the TestcPyConstruct class, the default copy constructor generated by the compilation was called, so errors occurred when the program test TestcPyConstruct object and Vector object.

This shows that the copy constructor is called when the element is inserted into the container, so when you store a custom type with a container, there is an error in the correct copy constructor.

Assignment operation

After reading the above instructions, we look at the assignment operation of the container, and the STL is supported by the container object mutual assignment.

Vector vect1;

Testcpyconstruct Obj;

Vect.push_back (obj);

Vector vect2;

Testcpyconstruct Obj2;

VECT2.PUSH_BACK (OBJ2);

Vect2 = VECT1;

The above errors also appear in this code because the custom class's Operator = function is called in the assignment operation in the VECTOR container, but there is an error because it is not defined.

In summary, STL has brought me great convenience, but it must be clear when using it, only this can write the correct program, especially when the application is in a custom type.

Ok, let's talk about it first, I know there are many. This article is for reference only. The rush of the article, there is a typison or error, please tell us to point out ccplusplus@21cn.com, thank you first. Welcome to communicate with you (- Yuan Xiakai -).

Attachment:

1. The following program call is a copy constructor instead of assignment operation.

Vector vect1;

Testcpyconstruct Obj;

Vect.push_back (obj);

Vector vect2;

Vect2 = VECT1;

2. TestcpyConstruct's copy constructor prototypes should be TestcPyconstruct (Const Testcpyconstruct &)

转载请注明原文地址:https://www.9cbs.com/read-5590.html

New Post(0)