From ecfd61c8d87e1127768afd7cf78ddd96e21a5f1f Mon Sep 17 00:00:00 2001 From: hsutter Date: Thu, 13 Sep 2018 13:22:23 -0700 Subject: [PATCH] Clarify C.128 Improved readability of primary paragraph. Added Discussion section of the classes of errors being addressed. --- CppCoreGuidelines.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 70201fb..9b7a10f 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -6786,7 +6786,12 @@ Readability. Detection of mistakes. Writing explicit `virtual`, `override`, or `final` is self-documenting and enables the compiler to catch mismatch of types and/or names between base and derived classes. However, writing more than one of these three is both redundant and a potential source of errors. -Use `virtual` only when declaring a new virtual function. Use `override` only when declaring an overrider. Use `final` only when declaring a final overrider. If a base class destructor is declared `virtual`, one should avoid declaring derived class destructors `virtual` or `override`. Some code base and tools might insist on `override` for destructors, but that is not the recommendation of these guidelines. +It's simple and clear: + - `virtual` means exactly and only "this is a new virtual function." + - `override` means exactly and only "this is a non-final overrider." + - `final` means exactly and only "this is a final overrider." + +If a base class destructor is declared `virtual`, one should avoid declaring derived class destructors `virtual` or `override`. Some code base and tools might insist on `override` for destructors, but that is not the recommendation of these guidelines. ##### Example, bad @@ -6813,6 +6818,14 @@ Use `virtual` only when declaring a new virtual function. Use `override` only wh // ... }; +#### Discussion + +We want to eliminate two particular classes of errors: + + - **implicit virtual**: the programmer intended the function to be implicitly virtual and it is (but readers of the code can't tell); or the programmer intended the function to be implicitly virtual but it isn't (e.g., because of a subtle parameter list mismatch); or the programmer did not intend the function to be virtual but it is (because it happens to have the same signature as a virtual in the base class) + + - **implicit override**: the programmer intended the function to be implicitly an overrider and it is (but readers of the code can't tell); or the programmer intended the function to be implicitly an overrider but it isn't (e.g., because of a subtle parameter list mismatch); or the programmer did not intend the function to be an overrider but it is (because it happens to have the same signature as a virtual in the base class -- note this problem arises whether or not the function is explicitly declared virtual, because the programmer may have intended to create either a new virtual function or a new nonvirtual function) + ##### Enforcement * Compare names in base and derived classes and flag uses of the same name that does not override.