开发者

C++ Abstract class construction and destruction

开发者 https://www.devze.com 2022-12-08 19:27 出处:网络
class base { base () { } virtual ~base () { } } class middleBase { middleBase () { } 开发者_如何学JAVA middleBase (int param) { }
class base
{
    base () { }
    virtual ~base () { }
}

class middleBase
{
    middleBase () { }
   开发者_如何学JAVA middleBase (int param) { }
    ~middleBase () { }
}

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

Error is : undefinded reference to "middleBase::middleBase (int param)"

and is this suitable for best practice for parameterized constructors?


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 */ }
};


c++ needs semicolons after class definitions:

class { … };


If you don't put access specifier (public/private/protected) before class' member functions or variables, the default is private specifier for C++ classes. So middleBase's constructor is private and cannot be access by concrete class' constructor, causing the error you mentioned.

0

精彩评论

暂无评论...
验证码 换一张
取 消