Virtual Base Bug
root
root@udev.yosemite.net
Thu Nov 4 06:32:00 GMT 1999
/*
* bug19991104.cc
*
* By: Mike Bird mgb@yosemite.net
*
* Class heirarchy: VB ... a virtual base class
* D1 D2
* X
* Y
*
* At least one piece of virtual base class information is being
* initialised too late so that a virtual call on an object of type Y
* currently constructed only as far as being an object of type X
* via a pointer of type D1 is not being adjusted correctly. I have a lot
* of similar code which works fine; it seems to be just this one case.
*
* Linux 2.2.12 on an i586
* First found in Redhat's 2.91.66 and reproduced in 2.95.2 built from scratch
*
* Compilation command:
* g++ -o bug19991104 bug19991104.cc
*
* Should produce: D1-1 D2-2 D1-1 D2-2 D1-1 D2-2
* Typically produces: D1-1 D2-2 D1-$ D2- D1-1 D2-2
*
* Once you've seen the problem, changing the cerr stuff to just ++ will
* allow you to remove the include file and make the problem much smaller,
* which is the reason I'm sending original C++ instead of pre-processed.
*/
#include <iostream>
struct VB {
virtual void F() = 0;
};
struct D1: virtual public VB {
char name1;
D1() : name1('1') {}
virtual void F() { cerr << " D1-" << name1; }
};
struct D2: virtual public VB {
char name2;
D2() : name2('2') {}
virtual void F() { cerr << " D2-" << name2; }
};
void G(D1* g)
{
g->F();
}
struct X: public D1, public D2 {
X() {
F(); // (1) This works
G(this); // (2) This produces garbage because something is not ready
}
void F() { D1::F(); D2::F(); }
};
struct Y: public X {
char something_is_needed_here_else_bug_wont_show;
Y() : X() {}
};
int main() {
Y y;
G(&y); // (3) This works because virtuals are ready, a little late
}
More information about the Gcc-bugs
mailing list