Re: Virtual bases and default move and swap functions [n2583, n2584]
Richard Smith wrote:
In a virtual inheritance hierarchy, the implicitly-declared assignment
operator is allowed to assign the base class multiple times
[[class.copy] 12.8/13]:
struct V {
V& operator=(V const&) { std::cout << "Foo!\n"; }
};
struct A : virtual V {};
struct B : virtual B {};
struct D : A,B {};
int main() { D d1, d2; d1 = d2; }
I assume you meant?:
struct V {
V& operator=(V const&) { std::cout << "Foo!\n"; return *this; }
};
struct A : virtual V { };
struct B : virtual A { };
struct D : virtual A, virtual B {};
In this example, it is unspecified how many times "Foo!" is printed
out, and many compilers will do the assignment twice. (In this
particular example, compliers could reasonably get it 'correct' and
only print "Foo!" once; add user-supplied assignment operators to A
and B and this is no longer the case.)
Apologies for digressing from your original question, but what's the
rational of the standard allowing this?!?
What's more, aren't the objects of the following classes exactly the
same (with the apparent difference in copy behavior)?? :
struct D_1 : B {};
struct D_2 : virtual B {};
struct D_3 : virtual A, B {};
struct D_4 : virtual A, virtual B {};
It certainly seems there is some hole in my understanding of virtual
inheritance ... ?
(...)
cheers,
Martin
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]