[clang-tools-extra] [clang-tidy] Improve documentation of bugprone-pointer-arithmetic-on-polymorphic-object (PR #108324)
Nicolas van Kempen via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 12 09:13:36 PDT 2024
================
@@ -19,20 +19,35 @@ Example:
.. code-block:: c++
struct Base {
- virtual void ~Base();
+ virtual ~Base();
+ int i;
};
-
+
struct Derived : public Base {};
-
- void foo() {
- Base *b = new Derived[10];
-
+
+ // Function that takes a pointer and performs pointer arithmetic
+ void foo(Base* b) {
b += 1;
// warning: pointer arithmetic on class that declares a virtual function can
// result in undefined behavior if the dynamic type differs from the
// pointer type
+ }
+
+ void bar() {
+ Derived d[10]; // Array of derived objects
+ foo(d); // Passing Derived pointer to foo(), which performs arithmetic
+ }
- delete[] static_cast<Derived*>(b);
+ // Another example showing array access with polymorphic objects.
----------------
nicovank wrote:
```suggestion
```
https://github.com/llvm/llvm-project/pull/108324
More information about the cfe-commits
mailing list