The topic is as follows, to output the result
classA { Public: A () {cout<<"A"<<Endl; } ~a () {cout<<"~a"<<Endl; } }; classB: PublicA { Public: B (A&a): _a (a) {cout<<"B"<<Endl; } ~B () {cout<<"~b"<<Endl; } Private: A _a; }; intMainvoid) {A A; b b (a); return 0; }
Here's the answer first:
[root]$./classAAB~B~a~a~a
You might find it strange, why not tune the constructor of a a once. In fact, this is due to the use of the initialization list, the initialization list in the initialization of the object of the class, the use of the copy constructor.
Modify the code to increase the printing of the copy constructor.
classA { Public: A () {cout<<"A"<<Endl; } ~a () {cout<<"~a"<<Endl; } A (Consta& a) {cout <<"Copy"<<Endl;} }; classB: PublicA { Public: B (A&a): _a (a) {cout<<"B"<<Endl; } ~B () {cout<<"~b"<<Endl; } Private: A _a; }; intMainvoid) {A A; b b (a); return 0; }
Then look at the results of the operation, so that the number of construction and destruction is consistent.
[root]$./classaacopyb~B~a~a~a
"C + +" a pen question about inheritance and destruction