Skip to content

Commit 8dbe756

Browse files
author
Colin Robertson
authored
Style, code updates
Update date, add description. Acrolinx fixes. Replace sample with one that demonstrates C26434 instead of C26435
1 parent 6eab855 commit 8dbe756

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

docs/code-quality/c26434.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,49 @@
11
---
22
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
45
ms.topic: "conceptual"
56
f1_keywords: ["C26434"]
67
helpviewer_keywords: ["C26434"]
78
ms.assetid: 7f66477f-da66-444a-a6e3-44513d7d7e31
89
---
910
# C26434 DONT_HIDE_METHODS
1011

11-
"Function hides a non-virtual function."
12+
> `Function 'derived::function' hides a non-virtual function 'base::function' (c.128).`
1213
1314
## C++ Core Guidelines
1415

1516
[C.128: Virtual functions should specify exactly one of virtual, override, or final](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md)
1617

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-
1918
## Remarks
2019

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.
2225
- Only non-virtual functions of base classes are considered.
2326
- No signature matching is performed. Warnings are emitted if unqualified names match.
2427

25-
## See also
28+
## Example
2629

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 good engineering practice for explicit **`override`** and **`virtual`** use:
2831

29-
# Example
30-
```
32+
```cpp
33+
// C26434.cpp
3134
struct Base
3235
{
33-
virtual void foo() = 0;
36+
virtual ~Base() = default;
37+
virtual void is_virtual() noexcept {}
38+
void not_virtual() noexcept {}
3439
};
3540

36-
struct Derived_1 : Base
41+
struct Derived : Base
3742
{
38-
virtual void foo() override {} // C26434, virtual is not needed here.
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
3947
};
4048

41-
struct Derived_2 : Base
42-
{
43-
void foo() override {}
44-
};
4549
```

0 commit comments

Comments
 (0)