Copy constructor, copy-assignment operator, and destructor

Source: Internet
Author: User

Read section 13.1 of C ++ primer today-copy, assign, and destroy

These things make me dizzy:


◆ Copy constructor

◆ The copy-assignment operator

◆ Destructor


The main problems are as follows:


◆ When do we need to rewrite it by ourselves?

◆ When will the system use our rewritten version?

◆ What is the distinction between copy construction and value assignment operators?


0x00 features

To distinguish them, we must start with each feature.

Can this refer to http://note.youdao.com/share? Id = 04efd559b42f4152423b3a2f08c86899 & type = Note


0x01 tricks

The above link is actually not readable, because I have summarized it myself and I still cannot figure it out.

It must be practical!


0x02 learning materials

Very good practice to explain the video https://www.youtube.com/watch? V = F-7Rpt2D-zo & noredirect = 1


0x03 a scenario where you do not need to write the above three items by yourself

In game design, we usually need some roles.

Each role protects its own features. First, we assume that:Name, life value, attack power, intelligence

class Character{public:    Character(string name, int life, int strength, int intelligence):      _name(name), _life(life), _strength(strength), _intelligence(intelligence){}private:    string _name;    int _life;    int _strength;    int _intelligence;};

If we need to copy the attribute of Role A to Role B

Int main () {character C1 ("Bob", 7, 5, 6); character C2 ("Sally", 8, 6, 6); C1 = c2; // call the system assembly copy-assignment operator}


0x04 is the scenario that requires self-Writing

Now, we add an attribute for the role:Item bar



Similarly, copy the attribute of Role A to Role B. If we continue to use the default system version, the system only copies the pointer content to C1.

As for content ...... Shared on the heap ......

c1=c2;


This assignment is called:


The danger is that if we delete the content of the role C2 after the role is copied, the C1 tool is all gone !!!

Imagine this is the role transaction and re-binding of RPG games, so ...... Let's wait for a complaint from the player !!!

# Include <iostream> # include <string> # define tool_size 5 using namespace STD; Class tool {public: tool () {for (INT I = 0; I <tool_size; I ++) {_ tool [I] = 0 ;}} void set_tool1 (INT tool [tool_size]) {for (INT I = 0; I <tool_size; I ++) {_ tool [I] = tool [I] ;}} PRIVATE: int _ tool [tool_size] ;}; class character {public: character (string name, int life, int strength, int intelligence, tool * toolarray): _ name (name), _ life (LIFE), _ strength (strength), _ intelligence (intelligence), _ toolarray (toolarray) {} PRIVATE: String _ name; int _ life; int _ strength; int _ intelligence; tool * _ toolarray;}; int main () {tool * T1 = new tool; int tool1 [5] = {11, 0, 0, 0}; T1-> set_tool1 (tool1); tool * t2 = new tool; int tool2 [5] = {22, 0}; t2-> set_tool1 (tool2); character C1 ("Bob", 7, 5, 6, T1); character C2 ("Sally", 8, 6, 6, t2); C1 = c2; // call the copy-assignment operatorcharacter C3 = c2 of the system assembly; // call the copy constructorreturn exit_success of the system assembly ;}


A: If we want to pass

c1=c2;
To obtain the following result, we need to reload opreator =


B: If we want to pass

Character c3 = c2;
To obtain the following result, we need to reload the copy constructor.

Why? Because opreator = is called only when the object already exists!



We have also mentioned the issue of deleting role C2. If we use a full copy, the Destructor assembled by the system will generate garbage during the deletion.


In this case, you need to write the destructor to delete them.


In summary, in order to effectively replicate and delete game roles:



This happens to be the rule of three.

# Include <iostream> # include <string> # define tool_size 5 using namespace STD; Class tool {public: tool () {for (INT I = 0; I <tool_size; I ++) {_ tool [I] = 0 ;}} tool (const Tool & T) {for (INT I = 0; I <tool_size; I ++) {_ tool [I] = T. _ tool [I] ;}} void set_tool (INT tool [tool_size]) {for (INT I = 0; I <tool_size; I ++) {_ tool [I] = tool [I] ;}} int get_tool (INT index) {return _ tool [Index] ;}private: int _ tool [tool_size] ;}; cl Ass character {public: character (string name, int life, int strength, int intelligence, tool * toolarray): _ name (name), _ life (LIFE ), _ strength (strength), _ intelligence (intelligence), _ toolarray (toolarray) {} character (const Character & C): _ name (C. _ name), _ life (C. _ life), _ strength (C. _ strength), _ intelligence (C. _ intelligence), _ toolarray (New Tool (* (C. _ toolarray) {cout <"copy constructor called! \ N ";} Character & operator = (const Character & C) {_ name = C. _ name; _ life = C. _ life; _ strength = C. _ strength; _ intelligence = C. _ intelligence; _ toolarray = new tool (* (C. _ toolarray); cout <"copy-assignment operator called! \ N "; return * This;} tool * get_tool_array () {return _ toolarray ;}~ Character () {Delete _ toolarray;} PRIVATE: String _ name; int _ life; int _ strength; int _ intelligence; tool * _ toolarray;}; int main () {tool * T1 = new tool; int tool1 [5] = {11, 0, 0, 0}; T1-> set_tool (tool1); tool * t2 = new tool; int tool2 [5] = {21,22, 23,0, 0}; t2-> set_tool (tool2); character C1 ("Bob", 7, 5, 6, T1 ); character C2 ("Sally", 8, 6, 6, T2); C1 = c2; // call the copy-assignment operatorcharacter C3 = c2 of the system assembly; // call copy constructorreturn exit_success ;}



(End)


Copy constructor, copy-assignment operator, and destructor

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.