Skip to content

Commit a676494

Browse files
author
Colin Robertson
authored
Merge pull request MicrosoftDocs#2408 from JordanMaples/patch-2
Add example for C26434
2 parents 18a0ac2 + 4723cd5 commit a676494

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

docs/code-quality/c26434.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +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 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

Comments
 (0)