Thread View: gnu.g++.help
2 messages
2 total messages
Started by Konstantin Laufer
Fri, 16 Oct 1992 20:48
Dispatching bug in g++?
Author: Konstantin Laufer
Date: Fri, 16 Oct 1992 20:48
Date: Fri, 16 Oct 1992 20:48
39 lines
1427 bytes
1427 bytes
Excuse me if this is already a well-known bug, but I don't read this
group regularly. Consider the following program:
#include <stream.h>
class P {
public:
virtual void hello() { cout << "hello\n"; }
void callhello() { hello(); }
};
class C : public P {
public:
virtual void hello() { cout << "hullo\n"; }
};
void g(P x) { x.callhello(); } // should call P::hello()
int main() {
C z;
z.hello(); // calls C::hello()
g(z); // results in an erroneous call to C::hello()!
}
When compiled with g++ 2.2.2 under SunOS 4.1.2, this program produces
the erroneous output
hullo
hullo
This is incorrect, since the x is a value parameter, whence
dispatching should be static.
Compilation with AT&T CC correctly results in
hullo
hello
Have other people observed this problem?
--
-Konstantin [laufer@math.luc.edu]
Re: Dispatching bug in g++?
Author: Joe Buck
Date: Fri, 16 Oct 1992 22:24
Date: Fri, 16 Oct 1992 22:24
53 lines
1775 bytes
1775 bytes
In article <1992Oct16.204850.3311@lucpum.it.luc.edu> laufer@math.luc.edu
submits the program
#include <stream.h>
class P {
public:
virtual void hello() { cout << "hello\n"; }
void callhello() { hello(); }
};
class C : public P {
public:
virtual void hello() { cout << "hullo\n"; }
};
void g(P x) { x.callhello(); } // should call P::hello()
int main() {
C z;
z.hello(); // calls C::hello()
g(z); // results in an erroneous call to C::hello()!
}
and writes:
>When compiled with g++ 2.2.2 under SunOS 4.1.2, this program produces
>the erroneous output
> hullo
> hullo
>
>This is incorrect, since the x is a value parameter, whence
>dispatching should be static.
>
>Compilation with AT&T CC correctly results in
> hullo
> hello
This looks like a fundamental bug in the default copy constructors
generated by g++ (determined by looking at the assembly language output).
They copy everything, including the pointer to the virtual function table.
This is wrong; the first word of the destination object should always be
the virtual function table for that object, regardless of what is copied
into it.
That is, when z is passed by value, its virtual function table pointer
is copied into x, the formal parameter for g(P), so that x is not
really a P at all, but rather a C.
--
Joe Buck jbuck@ohm.berkeley.edu
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads