Home > Back-end >  [C understand constructors, copy structure, destructor, copy assignment problem]
[C understand constructors, copy structure, destructor, copy assignment problem]

Time:03-26

C + + primer on exercise 13.13 write their own program to understand constructors, copy structure, destructor, copy assignment, but finish found that there are problems,
1. Why test1 (b) and test1 (* c) without any output?
2. Why only copy in pushing b into the container without destruction? Then why include * c in container copy destructor and copy?
 
# include & lt; iostream>
# include & lt; Vector>
Using STD: : cin; Using STD: : cout; Using STD: : string; Using STD: : endl; Using STD: : vector;
Struct X
{
X () {cout & lt; <"Structure" & lt; X (const X&) {cout & lt; <"Copy" & lt; X& Operator=(const X& X)
{
Cout & lt; <"Assignment" & lt; Return * this;
}
~ X () {cout & lt; <"Destruction" & lt; };
Void test1 (X& X) {return; }
Void test2 (X) X {return; }
Int main ()
{
A=new X X * ();
X b;
Delete a;
X * c=new X ();
Cout & lt; <"Test1 (b) : \ n";
Test1 (b);
Cout & lt; <"\ ntest1 (*) c: \ n";
Test1 (* c);
Cout & lt; <"\ ntest2 (b) : \ n";
Test2 (b);
Cout & lt; <"\ ntest2 (*) c: \ n";
Test2 (* c);
Cout & lt; <"\ nvec push_back (b) : \ n";
Vector Vec.
Vec. Push_back (b);
Cout & lt; <"\ nvec push_back () * c: \ n";
Vec. Push_back (* c);
system("pause");
return 0;
}

CodePudding user response:

Because test1 transfer reference, don't copy

CodePudding user response:

2, and the destructor to happen before the vec destructor, and vec destructor is the end of the main function will call