All derived classes must implement their own destructor functions, otherwise compile a report error?

Recently, the interviewer asked me the following question: "Derived classes allow inheritance from the base class, but all derived classes must implement a user-defined destructor. If the derived class does not define a user destructor definition, report a compilation error ." Any idea how to solve this problem?


We decided to solve this problem using a pure virtual destructor in the base class, but this does not solve the problem.

For the below code, I want the compiler to report an error since the derived class does not implement a user-defined destructor

class Base
{
public:
    virtual ~Base() = 0;
};

Base::~Base()
{
    cout << "Base destructor" << endl;
}

*//expecting an error here, since no user defined destructor*
class Derived : public Base 
{
};

int main()
{
    Derived d;
}
+4
1

class Enforcer {
  class InnerEnforcer {
  };
protected:
  ~Enforcer() throw(InnerEnforcer) {
    std::cout << "Enforcer::dtor" << std::endl;
  }
};

class Base : public virtual Enforcer {
public:
  virtual ~Base() throw() {
    std::cout << "Base::dtor" << std::endl;
  }
};

class Derived : public Base {
public:
  //~Derived() throw() { std::cout << "Derived:dtor" << std::endl; }
};

int main() {
  Derived d;

  Base* b = new Derived();
  delete b;

  return 0;
}

looser throw Derived:: ~ Derived() throw (Enforcer:: InnerEnforcer) '. .

EDIT: class Base : virtual Enforcer class Base : public virtual Enforcer

0

Source: https://habr.com/ru/post/1612352/


All Articles