title: Alternate to virtual function call on base destructor
body: <p>So, I've written the following code.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class AbstractMachine
  {
  public:
      void powerOn();
      void powerOff();
      bool isPoweredOn();
  
      virtual ~AbstractMachine();
  
  protected:
      virtual void powerOnImpl() = 0;
      virtual void powerOffImpl() = 0;
  
  private:
      bool m_isPoweredOn;
  };
  
  bool AbstractMachine::isPoweredOn()
  {
      return m_isPoweredOn;
  }
  
  void AbstractMachine::powerOn()
  {
      if (!m_isPoweredOn)
      {
          powerOnImpl();
          m_isPoweredOn = true;
      }
  }
  
  void AbstractMachine::powerOff()
  {
      if (m_isPoweredOn)
      {
          powerOffImpl();
          m_isPoweredOn = false;
      }
  }
  
  AbstractMachine::~AbstractMachine()
  {
      powerOff();       // (1)
      std::cout &lt;&lt; "Destroying a machine" &lt;&lt; std::endl;
  }
  
  class AirConditioner : public AbstractMachine
  {
  protected:
      void powerOnImpl() override;
      void powerOffImpl() override;
  public:
      ~AirConditioner();
  };
  
  void AirConditioner::powerOnImpl()
  {
      std::cout &lt;&lt; "Turning on air conditioner" &lt;&lt; std::endl;
  }
  
  void AirConditioner::powerOffImpl()
  {
      std::cout &lt;&lt; "Turning off air conditioner" &lt;&lt; std::endl;
  }
  
  AirConditioner::~AirConditioner()
  {
      //powerOff();        // (2)
      std::cout &lt;&lt; "Destroing air conditioner" &lt;&lt; std::endl;
  }
  
  
  int main()
  {
      AbstractMachine *machine = new AirConditioner();
      machine-&gt;powerOn();
      delete machine;
  }
  </code></pre>
  
  <p>This is going to fail, because derived object is already destroyed when the base destructor calls derived function on (1).</p>
  
  <p>This runs fine when I comment (1) and uncomment (2), but what I want to do here is to <strong>automatically and always turn off the machine before it gets destroyed, though the 'power off' action depends on the custom machine</strong>.</p>
  
  <p>Is there a better way of doing this?
  Should I just give up?
  Or is it not even the right way of a design on OOP?</p>