How to declare copy constructor in derived class, without default construcor in base?

倖福魔咒の 提交于 2019-11-30 06:46:07

Call the copy-constructor (which is generated by the compiler) of the base:

Derived( const Derived &d ) : Base(d)
{            //^^^^^^^ change this to Derived. Your code is using Base
    std::cout << "copy constructor\n";
}

And ideally, you should call the compiler generated copy-constructor of the base. Don't think of calling the other constructor. I think that would be a bad idea.

You can (and should) call the copy ctor of the base class, like:

Derived( const Derived &d ) :
        Base(d)
{
    std::cout << "copy constructor\n";
}

Note that I turned the Base parameter into a Derived parameter, since only that is called a copy ctor. But maybe you didn't really wanted a copy ctor...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!