summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/copy-assignment.html
blob: 540d6119d616f1a228289ef41a35e6b2662261c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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>