Bug in invoking virtual functions in g++ 2.8.1/egcs 1.0.3 release
Jonathan Mohr
mohrj@Berio.Augustana.AB.CA
Wed Aug 5 19:27:00 GMT 1998
Here is a simple example illustrating what I take to be a bug in
resolving which virtual function should be invoked. (This is a simpler
example than the one I previously announced on the 'egcs-bugs' mailing
list on 1998 July 30).
When the following program is compiled on an i586 machine running Linux
with g++ version 2.7.2.3, it (correctly) invokes Derived::operator= and
reports that A[0] contains 2. However, when compiled on a Cyrix 6x86
machine running Linux with egcs-2.90.29 980515 (egcs-1.0.3 release),
it invokes Base::operator= and reports that A[0] contains 1.
Can anyone else duplicate this result? Is this actually a compiler bug,
or was there some change in standard C++ that makes this program illegal?
----------------------- snip ------------------------------------
//
// A simple test of virtual overloading of operator= in C++.
//
// Under g++ 2.7.2.3, this program properly shows that A[0] contains 2.
// Under egcs-2.90.29 980515 (egcs-1.0.3 release) [and g++ 2.8.1],
// the program shows A[0] as containing 1 (i.e., it executes virtual
// function Base::op= instead of Derived::op=).
//
// J. Mohr 1998 August 5
//
#include <iostream.h>
class Base
{
public:
Base( int val = 0 ) { value = new int( val ); }
virtual ~Base() {}
virtual void operator= ( Base& b ) { *value = 1; }
int Value() { return *value; }
protected:
int* value;
};
class Derived : public Base
{
public:
Derived( int val = 0 ) : Base( val ) {}
virtual void operator= ( Base& b ) { *value = 2; }
};
class Array
{
public:
Array() { data = new Derived[ 1 ]; }
Derived & operator[] ( int i ) { return data[i]; }
protected:
Derived * data;
};
void main()
{
Derived d(0);
Array A;
A[0] = d;
cout << "A[0] contains " << A[0].Value() << " (should be 2)\n";
}
----------------------- snip ------------------------------------
Thanks in advance for any light you can shed.
Regards,
Jonathan Mohr
---
Jonathan Mohr, Associate Professor of Computing Science
Augustana University College, 4901-46 Ave., Camrose, AB, Canada T4V 2R3
E-mail: mohrj@augustana.ab.ca Phone: (403) 679-1514 Fax: (403) 679-1129
More information about the Gcc-bugs
mailing list