What does 'has virtual method … but non-virtual destructor' warning mean during C++ compilation?

后端 未结 3 1557
孤城傲影
孤城傲影 2020-12-13 06:05
#include 
using namespace std;

class CPolygon {
  protected:
    int width, height;
  public:
    virtual int area ()
      { return (0); }
  };

cl         


        
3条回答
  •  死守一世寂寞
    2020-12-13 06:50

    It merely means that a code like

    CPolygon* p = new CRectangle;
    delete p;
    

    ... or whatever wrapping into whatever smart pointer, will essentially not behave correctly since CPolygon is not polymorphic on deletion, and the CRectange part will not be destroyed properly.

    If you're not going to delete CRectangle and CPolygon polymorphicaly, that warning is not meaningful.

提交回复
热议问题