File tree Expand file tree Collapse file tree 1 file changed +14
-14
lines changed Expand file tree Collapse file tree 1 file changed +14
-14
lines changed Original file line number Diff line number Diff line change @@ -19,20 +19,28 @@ Example:
19
19
.. code-block :: c++
20
20
21
21
struct Base {
22
- virtual void ~Base();
22
+ virtual ~Base();
23
+ int i;
23
24
};
24
25
25
26
struct Derived : public Base {};
26
27
27
- void foo() {
28
- Base *b = new Derived[10];
29
-
28
+ void foo(Base* b) {
30
29
b += 1;
31
30
// warning: pointer arithmetic on class that declares a virtual function can
32
31
// result in undefined behavior if the dynamic type differs from the
33
32
// pointer type
33
+ }
34
+
35
+ int bar(const Derived d[]) {
36
+ return d[1].i; // warning due to pointer arithmetic on polymorphic object
37
+ }
34
38
35
- delete[] static_cast<Derived*>(b);
39
+ // Making Derived final suppresses the warning
40
+ struct FinalDerived final : public Base {};
41
+
42
+ int baz(const FinalDerived d[]) {
43
+ return d[1].i; // no warning as FinalDerived is final
36
44
}
37
45
38
46
Options
@@ -47,17 +55,9 @@ Options
47
55
48
56
.. code-block :: c++
49
57
50
- void bar() {
51
- Base *b = new Base[10];
58
+ void bar(Base b[], Derived d[]) {
52
59
b += 1; // warning, as Base declares a virtual destructor
53
-
54
- delete[] b;
55
-
56
- Derived *d = new Derived[10]; // Derived overrides the destructor, and
57
- // declares no other virtual functions
58
60
d += 1; // warning only if IgnoreVirtualDeclarationsOnly is set to false
59
-
60
- delete[] d;
61
61
}
62
62
63
63
References
You can’t perform that action at this time.
0 commit comments