summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/copy-assignment.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/gcc~13/copy-assignment.html
new repository
Diffstat (limited to 'devdocs/gcc~13/copy-assignment.html')
-rw-r--r--devdocs/gcc~13/copy-assignment.html34
1 files changed, 34 insertions, 0 deletions
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 @@
+<div class="subsection-level-extent" id="Copy-Assignment"> <div class="nav-panel"> <p> Previous: <a href="temporaries" accesskey="p" rel="prev">Temporaries May Vanish Before You Expect</a>, Up: <a href="c_002b_002b-misunderstandings" accesskey="u" rel="up">Common Misunderstandings with GNU C++</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="subsection" id="Implicit-Copy-Assignment-for-Virtual-Bases"><span>14.7.4 Implicit Copy-Assignment for Virtual Bases<a class="copiable-link" href="#Implicit-Copy-Assignment-for-Virtual-Bases"> ¶</a></span></h1> <p>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: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">struct Base{
+ char *name;
+ Base(const char *n) : name(strdup(n)){}
+ Base&amp; operator= (const Base&amp; 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 &amp;d1, Derived &amp;d2)
+{
+ d1 = d2;
+}</pre>
+</div> <p>The C++ standard specifies that ‘<samp class="samp">Base::Base</samp>’ is only called once when constructing or copy-constructing a Derived object. It is unspecified whether ‘<samp class="samp">Base::operator=</samp>’ is called more than once when the implicit copy-assignment for Derived objects is invoked (as it is inside ‘<samp class="samp">func</samp>’ in the example). </p> <p>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: ‘<samp class="samp">name</samp>’ (via <code class="code">strdup</code>), ‘<samp class="samp">val</samp>’, ‘<samp class="samp">name</samp>’ again, and ‘<samp class="samp">bval</samp>’. </p> <p>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. </p> </div><div class="_attribution">
+ <p class="_attribution-p">
+ &copy; Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
+ <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Copy-Assignment.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Copy-Assignment.html</a>
+ </p>
+</div>