duvarney@bert.dg.com (Dan DuVarney) (04/12/89)
It seems there is some bad interaction between private constructors and virtual functions. Consider the test program below: ----------- // This may look like C code, but it is really -*- C++ -*- #include <stdio.h> class foo { public: virtual unsigned x(); }; unsigned foo::x() { return 0; } class bar : public foo { bar(); // if this constructor is made public, everything works. public: virtual unsigned x(); }; bar::bar() {} unsigned bar::x() { return 1; } main() { bar x; foo& y = x; printf("x.x() == %d\n", x.x()); // this call works printf("y.x() == %d\n", y.x()); // this call produces a core dump } ------- I compile this under g++ 1.34.1, and link with libg++ 1.34.0. My build command is the following: ------- /pdd/pde2/gnu/g++-1.34.1/usr/local/bin/g++ -g -c -I/pdd/pde2/gnu/g++-1.34.1/usr/local/lib/g++-include vf_test.cc vf_test.cc:20: warning: class bar only defines private constructors and has no friends /pdd/pde2/gnu/g++-1.34.1/usr/local/bin/g++ -g -o vf_test -L/pdd/pde2/gnu/g++-1.34.1/usr/local/lib vf_test.o -lg++ ------- When I make bar's constructor public, I get the expected output: ------- x.x() == 1 y.x() == 2 ------- But when bar's constructor is private, I get: ------- y.x() == 1 Segmentation fault (core dumped) ------- It appears that the wrong address is being computed for virtual method x(). Dan DuVarney ...!mcnc!rti!dg-rtp!duvarney