C ++抽象类的构造和破坏

时间:2009-10-15 08:50:32

标签: c++

class base
{
    base () { }
    virtual ~base () { }
}

class middleBase
{
    middleBase () { }
    middleBase (int param) { }
    ~middleBase () { }
}

class concrete : public middleBase
{
    concrete () { }
    concrete (int param) { // process }
    ~concrete () { // delete something }
}

错误是:未定义引用“middleBase :: middleBase(int param)”

并且这适用于参数化构造函数的最佳实践吗?

3 个答案:

答案 0 :(得分:8)

class base
{
  public: // constructor should be accessible by derived class
    base () { }
    virtual ~base () { }
}; // add semicolon

class middleBase : public base // you missed the declaration
{
  public: 
    middleBase () { }
    middleBase (int param) { }
    virtual ~middleBase () { }
};

class concrete : public middleBase
{
  public:
    concrete () { }
    concrete (int param) : middleBase(param) { /* process */ }
    virtual ~concrete () { /* delete something */ }
};

答案 1 :(得分:3)

c ++在类定义后需要分号:

class { … };

答案 2 :(得分:0)

如果未在类的成员函数或变量之前放置访问说明符(public / private / protected),则缺省值为C ++类的private说明符。所以middleBase的构造函数是私有的,concrete class'构造函数无法访问它,导致你提到的错误。