|
1 | 1 | ---
|
2 | 2 | title: C26434
|
3 |
| -ms.date: 11/15/2017 |
| 3 | +description: "Microsoft C++ Code Analysis warning C26434 for the C++ Core Guidelines case C.128." |
| 4 | +ms.date: 08/21/2020 |
4 | 5 | ms.topic: "conceptual"
|
5 | 6 | f1_keywords: ["C26434"]
|
6 | 7 | helpviewer_keywords: ["C26434"]
|
7 | 8 | ms.assetid: 7f66477f-da66-444a-a6e3-44513d7d7e31
|
8 | 9 | ---
|
9 | 10 | # C26434 DONT_HIDE_METHODS
|
10 | 11 |
|
11 |
| -"Function hides a non-virtual function." |
| 12 | +> `Function 'derived::function' hides a non-virtual function 'base::function' (c.128).` |
12 | 13 |
|
13 | 14 | ## C++ Core Guidelines
|
14 | 15 |
|
15 | 16 | [C.128: Virtual functions should specify exactly one of virtual, override, or final](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md)
|
16 | 17 |
|
17 |
| -Introducing a function which has the same name as a non-virtual function in a base class is like introducing a variable name which conflicts with a name from outer scope. Furthermore, if signatures of functions mismatch, the intended overriding may turn into overloading. Overall, name hiding is dangerous and error-prone. |
18 |
| - |
19 | 18 | ## Remarks
|
20 | 19 |
|
21 |
| -- Only non-overriding functions in current class are checked. |
| 20 | +When you introduce a function that has the same name as a non-virtual function in a base class, you may get unexpected behavior. It's like introducing a variable name which conflicts with a name from an outer scope. For example, you may have intended to override a base class function. If the signatures of the functions don't match, the override you intended may turn into an overload instead. In general, name hiding is dangerous and error-prone. |
| 21 | + |
| 22 | +In the Core Guidelines checks: |
| 23 | + |
| 24 | +- Only non-overriding functions in the current class are checked. |
22 | 25 | - Only non-virtual functions of base classes are considered.
|
23 | 26 | - No signature matching is performed. Warnings are emitted if unqualified names match.
|
24 | 27 |
|
25 |
| -## See also |
| 28 | +## Example |
26 | 29 |
|
27 |
| -[C.128: Virtual functions should specify exactly one of virtual, override, or final](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md) |
| 30 | +This example demonstrates how a derived class can hide non-virtual functions, and how virtual functions allow both overloads and overrides: |
| 31 | + |
| 32 | +```cpp |
| 33 | +// C26434.cpp |
| 34 | +struct Base |
| 35 | +{ |
| 36 | + virtual ~Base() = default; |
| 37 | + virtual void is_virtual() noexcept {} |
| 38 | + void not_virtual() noexcept {} |
| 39 | +}; |
| 40 | + |
| 41 | +struct Derived : Base |
| 42 | +{ |
| 43 | + void is_virtual() noexcept override {} // Okay, override existing function |
| 44 | + virtual void is_virtual(int i) noexcept {} // Add a virtual overload for function |
| 45 | + void not_virtual() noexcept {} // C26434, hides a non-virtual function |
| 46 | + virtual void not_virtual(int i) noexcept {} // C26434, and parameters ignored |
| 47 | +}; |
| 48 | + |
| 49 | +``` |
0 commit comments