]> granicus.if.org Git - gc/commitdiff
Workaround 'class C does not have a copy constructor' cppcheck warning
authorIvan Maidanski <ivmai@mail.ru>
Thu, 3 May 2018 21:51:21 +0000 (00:51 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Fri, 6 Jul 2018 12:32:40 +0000 (15:32 +0300)
* tests/test_cpp.cc (class C): Add a copy constructor and an assignment
operator; add comment.

tests/test_cpp.cc

index 2b774fb0d0a9ecc3cbe72cf6af1f42723d8b1a30..2c5728fc25c445d30748d01336922dc6dcc1b80e 100644 (file)
@@ -103,6 +103,26 @@ int B::deleting = 0;
 class C: public GC_NS_QUALIFY(gc_cleanup), public A { public:
     /* A collectible class with cleanup and virtual multiple inheritance. */
 
+    // The class uses dynamic memory/resource allocation, so provide both
+    // a copy constructor and an assignment operator to workaround a cppcheck
+    // warning.
+    C(const C& c) : A(c.i), level(c.level) {
+        left = c.left ? new C(*c.left) : 0;
+        right = c.right ? new C(*c.right) : 0;
+    }
+
+    C& operator=(const C& c) {
+        if (this != &c) {
+            delete left;
+            delete right;
+            i = c.i;
+            level = c.level;
+            left = c.left ? new C(*c.left) : 0;
+            right = c.right ? new C(*c.right) : 0;
+        }
+        return *this;
+    }
+
     GC_ATTR_EXPLICIT C( int levelArg ): A( levelArg ), level( levelArg ) {
         nAllocated++;
         if (level > 0) {