I have following code
class base
{
private:
int k;
public:
base(const base& b){ this->k = b.k; cout<<" c-ctor "<<endl; }
base(int a = 10){ k = a; cout<<" a = "<<a<<endl; }
friend const ostream& operator<<(ostream& out, base& b)
{
return out<<b.k<<endl;
}
};
int main()
{
base b, b1(2);
vector<base> vec = {b, b1};
cout<<" check point "<<endl;
for(auto& elem : vec)
cout<<" "<<elem;
cout<<endl;
return 0;
}
Output :
1- a = 10
2- a = 2
3- c-ctor
4- c-ctor
5- c-ctor
6- c-ctor
7- check point
8- 10
9- 2
Could anybody please explain why 4 calls for copy constructor, i understand 2 calls while copying object in container. How 4?