Internal implementation of push_back (object) Saving object in list in STL
1. In the container, a copy of the object is stored
#include <iostream>#include<list>using namespacestd;classa{inti; Static intnum; Public: A (): I (0) {cout<<"A ()"<<endl; Num + +;} A (intII): I (ii) {cout<<"A (int)"<<endl; Num + +;} ~a () {cout<<"~a"<<Endl;} A (Consta&a) {i=A.I; cout<<"A (const a&)"<<Endl; Num++; } A&operator=(Consta&a) {cout<<"operator="<<Endl; I=A.I; return* This; } voidprint () {cout<<i<<Endl; } voidPrintn () {cout<<"num ="<<num<<Endl; }};intA::num =0;intMain () {//A (1); //a b = A; //A C; //C = A; //C.print (); //a.printn ();List<a>Li; Li.push_back (A (2)); List<a>::iterator it =Li.begin (); (*it). PRINTN ();} Output result: A (int)//a2 Temporary object
A (Consta&)//Li.push_back (A (2)) calls its copy constructor~a//leave temporary object scope A call its destructor num=2~A//program end from the output can be seen in the list is a copy of the object.
Internal implementation of push_back (object) Saving object in list in STL