isocpp / isocpp/CppCoreGuidelines
C.35: Why should a base class destructor be virtual, if the class doesn't have virtual functions?
@hsutter is already working on this.
Since Jan 17, 2019.
- Dominant language
- CSS
- Stars
- 45.3k
- Forks
- 5.6k
- PR merge metrics
- No merged PRs in 30d
Description
Concerning "C.35: A base class destructor should be either public and virtual, or protected and nonvirtual". I suspect this recommendation only makes sense for polymorphic hierarchies, which should be explicitly stated in the item.
As an example, consider cv::Scalar class from OpenCV. It derives from cv::Vec class, which makes sense, since a scalar essentially is a vector, with a fixed number of dimensions (four). But both classes can be owned, thus both have public destructors. Which are non-virtual, since there are no virtual functions in the whole hierarchy. Furthermore, binary layout of the classes is often considered to have exactly the data members, and vtable would be definitely not convenient. Both classes are mostly used as values, you can rarely see cv::Vec*, where you can place an instance of cv::Scalar.
I'm not saying this design is perfect, but it seems reasonable. Should we declare it bad? Should we say it violates the guidelines and should be avoided?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.