Constructor call when copying a class to a vector

I have the 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  

Can someone explain why there are 4 calls for the copy constructor, I understand 2 calls when copying an object in a container. How 4?

+4
source share
1 answer

The reason is that initialization vector<base> vec = {b, b1};creates std::initializer_list<base>and passes it to the corresponding vector constructor. Then the copy will continue.

You can limit the number of copies by directly initializing members std::initializer_list<base>, instead of creating named objects. Something like that:

vector<base> vec = {{}, {2}};

reserve , emplace .

vector<base> vec;
vec.reserve(2);
vec.emplace_back();
vec.emplace_back(2);
+8

Source: https://habr.com/ru/post/1663821/


All Articles