dimanche 10 novembre 2019

Copy list initialization

I am learning C++, but there is a problem about copy initialization which really confuses me. The following is the class that I created.

class Example{
public:
    Example(initializer_list<int> il) {cout<<"constructor"<<endl;}
    Example(const char *) {cout<<"constructor"<<endl;}
    Example(const Example &) {cout<<"copy constructor"<<endl;}
};

Constructor and copy constructor will be called if the code looks like this, Example ex = "Hello world". However, if the code is Example ex = {0,1,2,3}, only constructor will be called, not copy constructor. Any explains? I'd really appreciate.

Aucun commentaire:

Enregistrer un commentaire