Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!uunet!mcvax!hp4nl!kunivv1!atcmpe!leo From: leo@atcmp.nl (Leo Willems) Newsgroups: comp.lang.c++ Subject: Virtual table problem Keywords: virtual pointer table Message-ID: <527@atcmpe.atcmp.nl> Date: 22 Jun 89 12:27:22 GMT Organization: AT Computing, Nijmegen, The Netherlands Lines: 80 I recently put a question on the net, which, i think, did not reach many places, or the right readers. (When re-reading this example i discovered a bug of which i'm not sure i corrected it in my initial posting, to be sure i marked the line in this posting. Sorry for that) Since then, i found that my problem which (still) exists for our C++ compiler (a version of cfront) does compile correct under G++. So here it is again, if it's a stupid question, please e-flame anyway so i now this message got out. I recently made a dispatch function which calls a memberfunction from some class B. This code is shown below class B{ public: void put() { puts("BBBBBB"); } }; typedef void (B::*PF)(); void disp(B* bp, PF f) { (bp->*f)(); } main() { B ab; disp(&ab, &B::put); } That worked quit well. After that i extended the B class with a virtual member put(). class B{ public: virtual void put() { puts("BBBBBB"); } // BUG: the previous // version did not // have the word // virtual }; Our C++ compiler (1.2E) did not complain, but our C compiler did. The C code generated was somewhat complex, using a ternary operator. Sorry, to put this in, but explains the error. void disp (_au0_bp , _au0_f ) struct B *_au0_bp ; PF _au0_f ; { (*(((void (*)(/* struct B* */))(((((unsigned int )_au0_f ))& (~ 127))? _au0_f : (_au0_bp -> _B__vptr [(((unsigned int )_au0_f ))- 1])))))( _au0_bp ) ; } Our C compiler is complaining that the =false= part of the ternary operator is of the wrong pointer type, which is true i think: true part: _au0_f is of type PF. false part: this gives a pointer of type int (*)(); (it is one of the elements of the vtable from B: implemented as: int (**_B__vptr)(); ) If i patch the generated code witch a cast (PF) in front of the false part everything works fine :-( Is there any chance that my solution (without a cast!) should work? Or is this kind of solution not supposed to work for classes with virtuals? (it works fine in G++) Thanks, any reaction is welcome. Leo Willems Internet: leo@atcmp.nl AT Computing UUCP: mcvax!hp4nl!kunivv1!atcmpe!leo P. O. Box 1428 6501 BK Nijmegen The Netherlands