相关疑难解决方法(0)

如何在派生类中声明复制构造函数,而不在基础中使用默认的construcor?

请看下面的例子:

class Base
{
protected:
    int m_nValue;

public:
    Base(int nValue)
        : m_nValue(nValue)
    {
    }

    const char* GetName() { return "Base"; }
    int GetValue() { return m_nValue; }
};

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

    const char* GetName() { return "Derived"; }
    int GetValueDoubled() { return m_nValue * 2; }
};
Run Code Online (Sandbox Code Playgroud)

这段代码不断给我一个错误,即基类没有默认的构造函数.当我宣布一切都好的时候.但是当我不这样做时,代码不起作用.

如何在派生类中声明复制构造函数而不在基类中声明默认的构造函数?

Thnaks.

c++ inheritance copy-constructor

14
推荐指数
2
解决办法
3万
查看次数

标签 统计

c++ ×1

copy-constructor ×1

inheritance ×1