This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Bug in invoking virtual functions in g++ 2.8.1/egcs 1.0.3 release

Jonathan Mohr <mohrj@Berio.Augustana.AB.CA> writes:

> When the following program is compiled on an i586 machine running Linux
> with g++ version, it (correctly) invokes Derived::operator= and

The old behavior is incorrect.  Because you have not defined
Derived::operator=([const] [volatile] Derived&), a ISO C++ compiler is
required to implicitly define one, and so does egcs.  The assignment
operator is implicitly defined so that, for each base class, its
assignment operator is invoked ``as if by explicit qualification, that
is, ignoring any possible virtual overriding functions in more derived
classes'' [class.copy]/13.  Looks like 13 is not your lucky number :-)

> #include <iostream.h>
> class Base { public: Base() { value = 0; } virtual void operator= (
> Base& b ) { value = 1; } int value; };
> class Derived : public Base { public: virtual void operator= ( Base&
> b ) { value = 2; } };
> void main() { Derived x, d; x = d; cout << "x contains " << x.value
> << " (should be 2).\n"; }

Alexandre Oliva
Universidade Estadual de Campinas, SP, Brasil

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]