Search code examples
c++visual-c++destructorcompiler-warnings

Why is C4265 Visual C++ warning (virtual member function and no virtual destructor) off by default?


Visual C++ can emit C4265 warning when a class has at least one virtual member function and no virtual destructor.

Obviously the warning is there to detect cases when a derived class object is deleted through a pointer to base class and there's no virtual destructor in the base class. Such situation yields undefined behavior. Btw I've just found an example of this situation in a rather huge codebase precisely by enabling C4265 in Visual C++.

And this warning is off by default.

Why? What would happen if I enabled it and added a virtual destructor to each class where the warning is emitted?


Solution

  • In the link for the documentation for that warning it explains Microsoft's reasoning. If a warning is off by default, most users would not want to see it.