Skip to content

Commit e1f6e5a

Browse files
authored
Update C26474 to clarify base/derived
Updating documentation regarding base/derived casts. Additionally, updated the indentation in the provided examples.
1 parent a757e90 commit e1f6e5a

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

docs/code-quality/c26474.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ In some cases, implicit casts between pointer types can safely be done and don
2121
- The rule ID is a bit misleading: it should be interpreted as "implicit cast is not used where it is acceptable".
2222
- The rule is applicable to pointers only and checks static casts and reinterpret casts.
2323
- The following cases are acceptable pointer conversions that should not use explicit cast expressions:
24-
- conversion to nullptr_t;
25-
- conversion to void*;
26-
- conversion from derived type to its base.
24+
- conversion to nullptr_t;
25+
- conversion to void*;
26+
- conversion from a derived type to its base when invoking a base member function that is not hidden by the derived type.
2727

28-
## Example
28+
## Example 1
2929

3030
unnecessary conversion hides logic error
3131

@@ -58,3 +58,24 @@ bool register_buffer(T *buffer) {
5858
}
5959
// ...
6060
```
61+
62+
## Example 2
63+
Using casts to access base-class member functions
64+
```cpp
65+
struct struct_1
66+
{
67+
void foo();
68+
void bar();
69+
}
70+
struct struct_2 : struct_1
71+
{
72+
void foo(); // this definition hides struct_1::foo
73+
}
74+
75+
void fn(struct_2* ps2)
76+
{
77+
static_cast<struct_1*>(ps2)->foo(); // this cast is necessary to access `struct_1::foo`
78+
// alternatively ps2->struct_1::foo();
79+
static_cast<struct_1*>(ps2)->bar(); // this cast is unnecessary and can be done implicitly
80+
}
81+
```

0 commit comments

Comments
 (0)