Skip to content

Commit 1753ea0

Browse files
authored
Merge pull request #3099 from JordanMaples/patch-12
Add example and Core Guidelines link to C26436
2 parents 2be27ed + 49ae43b commit 1753ea0

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

docs/code-quality/c26436.md

+31-2
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,46 @@ ms.topic: "conceptual"
55
f1_keywords: ["C26436"]
66
helpviewer_keywords: ["C26436"]
77
ms.assetid: 82d14d5d-5c5d-4e27-bdc8-268f9973a312
8+
description: CppCoreCheck rule that enforces C++ Core Guidelines C.35
89
---
910
# C26436 NEED_VIRTUAL_DTOR
1011

1112
"The type with a virtual function needs either public virtual or protected nonvirtual destructor."
1213

13-
**C++ Core Guidelines**:
14-
C.35: A base class destructor should be either public and virtual, or protected and nonvirtual
14+
[**C++ Core Guidelines**: C.35](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c35-a-base-class-destructor-should-be-either-public-and-virtual-or-protected-and-non-virtual): A base class destructor should be either public and virtual, or protected and nonvirtual
1515

1616
If a class defines a virtual function it becomes polymorphic, which implies that derived classes can change its behavior including resource management and destruction logic. Because client code may call polymorphic types via pointers to base classes, there is no way a client can explicitly choose which behavior is appropriate without downcasting. To make sure that resources are managed consistently and destruction occurs according to the actual type's rules it is recommended to define a public virtual destructor. If the type hierarchy is designed to disallow client code to destroy objects directly, destructors should be defined as protected non-virtual.
1717

1818
## Remarks
1919

2020
- The warning shows up on the first virtual function definition of a type (it can be a virtual destructor if it is not public), once per type.
2121
- Since definition can be placed separately from declaration, it may not always have any of the virtual specifiers. But the warning is still valid – it checks the actual 'virtuality' of a function.
22+
23+
## Example
24+
```cpp
25+
namespace no_destructor
26+
{
27+
struct base {
28+
virtual void foo() {} // C26436, see remarks to understand the placement of the warning.
29+
};
30+
}
31+
```
32+
33+
The warning does not appear when the base class has either a virtual public destructor or a protected non-virtual destructor.
34+
```cpp
35+
namespace virtual_destructor
36+
{
37+
struct base {
38+
virtual ~base();
39+
virtual void foo() {}
40+
};
41+
}
42+
namespace protected_destructor
43+
{
44+
struct base {
45+
virtual void foo() {}
46+
protected:
47+
~base() {}
48+
};
49+
}
50+
```

0 commit comments

Comments
 (0)