GNU compiler warning “class has virtual functions but non-virtual destructor”

前端 未结 7 2347
有刺的猬
有刺的猬 2020-12-13 06:08

I have defined an interface in C++, i.e. a class containing only pure virtual functions.

I want to explicitly forbid users of the interface to delete the object thro

相关标签:
7条回答
  • 2020-12-13 06:36

    Some of the comments on this answer relate to an earlier answer I gave, which was wrong.

    A protected destructor means that it can only be called from a base class, not through delete. That means that an ITest* cannot be directly deleted, only a derived class can. The derived class may well want a virtual destructor. There is nothing wrong with your code at all.

    However, since you cannot locally disable a warning in GCC, and you already have a vtable, you could consider just making the destructor virtual anyway. It will cost you 4 bytes for the program (not per class instance), maximum. Since you might have given your derived class a virtual dtor, you may find that it costs you nothing.

    0 讨论(0)
提交回复
热议问题