Home > Back-end >  Consult a STD: : move and move structure problem
Consult a STD: : move and move structure problem

Time:11-17

Defines a base class, and constructors, copy constructors, mobile constructor, destructor,
 class base {
Public:
The base () : c (new int (1)), s (" x ") {
Cout & lt; <"Defaut constructed base" & lt; };
Int * c;
string s;

The base (base& & Bas) : c (bas) c) and s (bas) s) {
Bas. C=nullptr;
Cout & lt; <"Move the construct" & lt; }

The base (base& BBB) {
C=new int (* BBB. C);
S=BBB. S;
Cout & lt; <"Copy the construct" & lt; }

~ base () {
Cout & lt; <"Delete" & lt; The delete c;

Cout & lt; <"Deconstruct base" & lt; }


};


The following operation
 base ba1. 
Base & amp; & Ba2=STD: : move (ba1);
Base ba3 (STD: : move (ba1));
Ba2. ~ base ();
Ba1. ~ base ();


Don't complain, from print to see a total of four destructor, why? If only one ba2 or destructor ba1, can also be normal,
 delete0 
Deconstruct base
Delete0
Deconstruct base

Delete0x133cc20
Deconstruct base
Delete0
Deconstruct base




If according to the following operation complains double free:
 base ba1. 
Cout & lt; Base & amp; & Ba2=STD: : move (ba1);
Ba2. ~ base ();


I understand because ba2 and ba1 refer to the same area, so there will be a repeating mistakes, that is what mobile structure, can repeat release?

CodePudding user response:

Value type of the data, there is no need to call the destructor, the program will automatically call them, you call twice, so according to call four
You there are only two classes, a ba1, one is via mobile tectonic ba3,

CodePudding user response:

reference 1/f, the truth is right or wrong response:
value type of the data, there is no need to own the destructor, the program will automatically call them, you call twice, so according to call four
You there are only two classes, a ba1, one is via mobile tectonic ba3,

Intermediate destructor is I intentionally calls to see the results, the four destructor destructor which several objects are respectively? Destructor, between me and the final system and a destructor, whether there is a repeated destructor?

CodePudding user response:

Base ba1.
Base & amp; & Ba2=STD: : move (ba1);
Base ba3 (STD: : move (ba1));
Ba2. ~ base ();//1
Ba1. ~ base ();//2
Ba1 survival, automatic call back again
Ba3//same as above
A total of four times
  • Related