mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
Clarify C.128
Improved readability of primary paragraph. Added Discussion section of the classes of errors being addressed.
This commit is contained in:
parent
104cd63a37
commit
ecfd61c8d8
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue
Block a user