294
11.9.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(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;
}
The C++ standard specifies that
copy-constructing a Derived object. It is unspecified whether
once when the implicit copy-assignment for Derived objects is invoked (as it is inside
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:
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.
11.10. Caveats of using
The conversion programs
that won't work unless you rearrange it.
can insert references to a type name or type tag before the definition, or in a file where
•
protoize
they are not defined.
If this happens, compiler error messages should show you where the new references are, so fixing
the file by hand is straightforward.
Base::Base
protoize
and
protoize
unprotoize
Chapter 11. Known Causes of Trouble with GCC
is only called once when constructing or
Base::operator=
,
(via
val
name
strdup
can sometimes change a source file in a way
is called more than
in the
func
),
, and
again.
bval
name
Need help?
Do you have a question about the ENTERPRISE LINUX 4 and is the answer not in the manual?
Questions and answers