diff options
author | Martin v. Löwis <loewis@informatik.hu-berlin.de> | 1999-03-26 00:58:14 +0000 |
---|---|---|
committer | Martin v. Löwis <loewis@gcc.gnu.org> | 1999-03-26 00:58:14 +0000 |
commit | 5197829d3cac9e363ee2c3bdd62999206b5cd2e2 (patch) | |
tree | 138dafe05e32b1894dc59d93ac353706522dc005 /gcc/gcc.texi | |
parent | dd4b5f0d25ba0dd80d105e0b29b4be3f8b6d333e (diff) | |
download | gcc-5197829d3cac9e363ee2c3bdd62999206b5cd2e2.zip gcc-5197829d3cac9e363ee2c3bdd62999206b5cd2e2.tar.gz gcc-5197829d3cac9e363ee2c3bdd62999206b5cd2e2.tar.bz2 |
gcc.texi (Copy Assignment): New node.
* gcc.texi (Copy Assignment): New node.
* gxxint.texi: Remove old discussion on copying virtual bases.
From-SVN: r25992
Diffstat (limited to 'gcc/gcc.texi')
-rw-r--r-- | gcc/gcc.texi | 66 |
1 files changed, 61 insertions, 5 deletions
diff --git a/gcc/gcc.texi b/gcc/gcc.texi index da8431f..4f9ceeb 100644 --- a/gcc/gcc.texi +++ b/gcc/gcc.texi @@ -1643,15 +1643,16 @@ in scope at the time of the call. @cindex misunderstandings in C++ @cindex surprises in C++ @cindex C++ misunderstandings -C++ is a complex language and an evolving one, and its standard definition -(the ANSI C++ draft standard) is also evolving. As a result, -your C++ compiler may occasionally surprise you, even when its behavior is -correct. This section discusses some areas that frequently give rise to -questions of this sort. +C++ is a complex language and an evolving one, and its standard +definition (the ISO C++ standard) was only recently completed. As a +result, your C++ compiler may occasionally surprise you, even when its +behavior is correct. This section discusses some areas that frequently +give rise to questions of this sort. @menu * Static Definitions:: Static member declarations are not definitions * Temporaries:: Temporaries may vanish before you expect +* Copy Assignment:: Copy Assignment operators copy virtual bases twice @end menu @node Static Definitions @@ -1746,6 +1747,61 @@ string& tmp = strfunc (); charfunc (tmp.c_str ()); @end example +@node Copy Assignment +@subsection 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: + +@example +struct Base@{ + char *name; + Base(char *n) : name(strdup(n))@{@} + Base& operator= (const Base& other)@{ + free (name); + name = strdup (other.name); + @} +@}; + +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; +@} +@end example + +The C++ standard specifies that @samp{Base::Base} is only called once +when constructing or copy-constructing a Derived object. It is +unspecified whether @samp{Base::operator=} is called more than once when +the implicit copy-assignment for Derived objects is invoked (as it is +inside @samp{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 many times. In the example, copying +proceeds in the following order: @samp{val}, @samp{name} (via +@code{strdup}), @samp{bval}, and @samp{name} again. + +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. + @node Protoize Caveats @section Caveats of using @code{protoize} |