Why aren't copy constructors “chained” like default constructors and destructors?

后端 未结 3 2060
死守一世寂寞
死守一世寂寞 2020-12-05 03:18

Why aren\'t copy constructors chained (like default ctors or dtors) so that before the derived class\'s copy constructor is called, the base class\'s copy constructor is cal

3条回答
  •  萌比男神i
    2020-12-05 03:40

    You can:

    Derived(const Derived& d) : Base(d) {
        cout << "Derived::Derived(const B&)" << endl;
    }
    

    This calls the Base copy constructor on the Base sub-object of d.

    The answer for 'why' I don't know. But usually there's no answer. The committee just had to choose one option or the other. This seems more consistent with the rest of the language, where e.g. Derived(int x) won't automatically call Base(x).

提交回复
热议问题