File tree Expand file tree Collapse file tree 1 file changed +25
-4
lines changed Expand file tree Collapse file tree 1 file changed +25
-4
lines changed Original file line number Diff line number Diff line change @@ -21,11 +21,11 @@ In some cases, implicit casts between pointer types can safely be done and don
21
21
- The rule ID is a bit misleading: it should be interpreted as "implicit cast is not used where it is acceptable".
22
22
- The rule is applicable to pointers only and checks static casts and reinterpret casts.
23
23
- 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.
27
27
28
- ## Example
28
+ ## Example 1
29
29
30
30
unnecessary conversion hides logic error
31
31
@@ -58,3 +58,24 @@ bool register_buffer(T *buffer) {
58
58
}
59
59
// ...
60
60
```
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
+ ```
You can’t perform that action at this time.
0 commit comments