From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/gcc~13/copy-assignment.html | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 devdocs/gcc~13/copy-assignment.html (limited to 'devdocs/gcc~13/copy-assignment.html') diff --git a/devdocs/gcc~13/copy-assignment.html b/devdocs/gcc~13/copy-assignment.html new file mode 100644 index 00000000..540d6119 --- /dev/null +++ b/devdocs/gcc~13/copy-assignment.html @@ -0,0 +1,34 @@ +

14.7.4 Implicit Copy-Assignment for Virtual Bases

When a base class is virtual, only one subobject of the base class belongs to each full object. Also, the constructors and destructors are invoked only once, and called from the most-derived class. However, such objects behave unspecified when being assigned. For example:

struct Base{
+  char *name;
+  Base(const char *n) : name(strdup(n)){}
+  Base& operator= (const Base& other){
+   free (name);
+   name = strdup (other.name);
+   return *this;
+  }
+};
+
+struct A:virtual Base{
+  int val;
+  A():Base("A"){}
+};
+
+struct B:virtual Base{
+  int bval;
+  B():Base("B"){}
+};
+
+struct Derived:public A, public B{
+  Derived():Base("Derived"){}
+};
+
+void func(Derived &d1, Derived &d2)
+{
+  d1 = d2;
+}
+

The C++ standard specifies that ‘Base::Base’ is only called once when constructing or copy-constructing a Derived object. It is unspecified whether ‘Base::operator=’ is called more than once when the implicit copy-assignment for Derived objects is invoked (as it is inside ‘func’ in the example).

G++ implements the “intuitive” algorithm for copy-assignment: assign all direct bases, then assign all members. In that algorithm, the virtual base subobject can be encountered more than once. In the example, copying proceeds in the following order: ‘name’ (via strdup), ‘val’, ‘name’ again, and ‘bval’.

If application code relies on copy-assignment, a user-defined copy-assignment operator removes any uncertainties. With such an operator, the application can define whether and how the virtual base subobject is assigned.

+

+ © Free Software Foundation
Licensed under the GNU Free Documentation License, Version 1.3.
+ https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Copy-Assignment.html +

+
-- cgit v1.2.3