What is the proper way of pushing a new object element onto a std::vector
? I want the data to be allocated in the vector. Will this copy the object newrad
Try this:
#include
#include
class base
{
int i;
public:
base(int z=0){i=z;cout<<"okk constructor "< basev;
base baseobj1(1);
base baseobj2(2);
base baseobj3(3);
base baseobj4(4);
base baseobj5(5);
base baseobj6(6);
base baseobj7(7);
base baseobj8(8);
base baseobj9(9);
base baseobj10(10);
basev.push_back(baseobj1);
cout<<"second push back\n";
basev.push_back(baseobj2);
cout<<"third push back\n";
basev.push_back(baseobj3);
cout<<"fourth push back\n";
basev.push_back(baseobj4);
cout<<"fifth push back\n";
basev.push_back(baseobj5);
cout<<"sixth push back\n";
basev.push_back(baseobj6);
cout<<"seventh push back\n";
basev.push_back(baseobj7);
cout<<"eighth push back\n";
basev.push_back(baseobj8);
cout<<"ninth push back\n";
basev.push_back(baseobj9);
cout<<"10th push back\n";
basev.push_back(baseobj10);
cout<<"after all push back\n";
cout<<"before clear\n";
basev.clear();
cout<<"after clear\n";
}
output:
before anything
okk constructor 1 called
okk constructor 2 called
okk constructor 3 called
okk constructor 4 called
okk constructor 5 called
okk constructor 6 called
okk constructor 7 called
okk constructor 8 called
okk constructor 9 called
okk constructor 10 called
copy constructor 1 called
second push back
copy constructor 1 called
copy constructor 2 called
destructor of 1 base called
third push back
copy constructor 1 called
copy constructor 2 called
copy constructor 3 called
destructor of 1 base called
destructor of 2 base called
fourth push back
copy constructor 4 called
fifth push back
copy constructor 1 called
copy constructor 2 called
copy constructor 3 called
copy constructor 4 called
copy constructor 5 called
destructor of 1 base called
destructor of 2 base called
destructor of 3 base called
destructor of 4 base called
sixth push back
copy constructor 6 called
seventh push back
copy constructor 7 called
eighth push back
copy constructor 8 called
ninth push back
copy constructor 1 called
copy constructor 2 called
copy constructor 3 called
copy constructor 4 called
copy constructor 5 called
copy constructor 6 called
copy constructor 7 called
copy constructor 8 called
copy constructor 9 called
destructor of 1 base called
destructor of 2 base called
destructor of 3 base called
destructor of 4 base called
destructor of 5 base called
destructor of 6 base called
destructor of 7 base called
destructor of 8 base called
10th push back
copy constructor 10 called
after all push back
before clear
destructor of 1 base called
destructor of 2 base called
destructor of 3 base called
destructor of 4 base called
destructor of 5 base called
destructor of 6 base called
destructor of 7 base called
destructor of 8 base called
destructor of 9 base called
destructor of 10 base called
after clear
destructor of 10 base called
destructor of 9 base called
destructor of 8 base called
destructor of 7 base called
destructor of 6 base called
destructor of 5 base called
destructor of 4 base called
destructor of 3 base called
destructor of 2 base called
destructor of 1 base called