Adress of a method
Olaf Flebbe
O.Flebbe@science-computing.de
Thu Jan 28 23:30:00 GMT 1999
Hi,
** egcs 1.1.1 on Linux compiled with gcc bof.cc ***
There may be a bug in determing the adress of a method. It seems to
me that that the last declared method will be choosen, regardless of
it signature.
The following code is not preprocessed because it does not depend on
the iostream header. The `cout's' are only included for demonstration
purposes. Please see the attached code and compile it with g++ bof.cc:
It will output a dubious warning while compiling with egcs 1.1.1
----output of egcs-1.1.1----
bof.cc: In method `bof::bof()':
bof.cc:30: warning: converting from `void (bof::*)()' to `void (*)(void *)'
---------------------------
The a.out will output when compiled with egcs-1.1.1
-----output of egcs compiled------
Virtual Callback
----------------------------------
gcc-2.7.2 and HP's aCC will generate no such warning and the
executable will output:
-----output of gcc compiled a.out---
Static Callback
Virtual Callback
----------------------
This example is modelled after code generated by Xdesigner 4.6c. The
Callbacks are Motif Callbacks in reality. The code generated by egcs
crashes my program heavily.
The workaround is to exchange the definition of both callbacks.
Cheers
Olaf
---- bof.cc --
#include <iostream.h>
typedef void (*CallbackProc)(void *);
class bof {
public:
bof();
// Exchange these two lines
static void callback( void * instance);
virtual void callback();
};
CallbackProc func;
void *data;
void registercallback( CallbackProc ptr, void * dat) {
func = ptr;
data = dat;
}
void triggercallback() {
(*func)( data);
}
bof::bof() {
registercallback( &callback, this);
}
void bof::callback( void * inst) {
cout << "Static Callback" << endl;
bof *instance = (bof *) inst;
instance->callback();
}
void bof::callback() {
cout << "Virtual Callback" << endl;
}
int main() {
bof o;
triggercallback();
}
---end of bof ---
---
Dr. Olaf Flebbe Phone +49 (0)7071-9457-32
science + computing gmbh FAX +49 (0)7071-9457-27
Hagellocher Weg 71
D-72070 Tuebingen Email: o.flebbe@science-computing.de
Windows NT, from the guys who brought you edlin
More information about the Gcc-bugs
mailing list