🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.bug
Article #1010

wrong virtual called in MI cases

#1010
From: enewton
Date: Fri, 02 Oct 1992 13:55
115 lines
3106 bytes
gcc2.2.2 configured for sco, native assembler and linker.

I get unexpected output from the program given below.  I expect the
three virtual functions called "create" to get called once.  I only
get the create for class "E".  If I move the constructor

C::C {}

around I get different results.  If I switch the definitions of D and
E around, the output changes again.

-Eric uunet.uu.net!srg!enewton

----------------------------------------------------------------------
$ gcc2 -v -xc++ testit.c
Reading specs from /usr/local/lib/gcc-lib/sco/2.2.2/specs
gcc version 2.2.2
 /usr/local/lib/gcc-lib/sco/2.2.2/cpp -lang-c++ -v -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -Dunix -Di386 -DM_UNIX -DM_I386 -DM_COFF -DM_WORDSWAP -Dsco -DSCO -D__unix__ -D__i386__ -D__M_UNIX__ -D__M_I386__ -D__M_COFF__ -D__M_WORDSWAP__ -D__sco__ -D__SCO__ -D__unix -D__i386 -D__M_UNIX -D__M_I386 -D__M_COFF -D__M_WORDSWAP -D__sco -D__SCO -nostdinc -I/usr/include testit.c /usr/tmp/cca11994.i
GNU CPP version 2.2.2 (80386, ATT syntax)
 /usr/local/lib/gcc-lib/sco/2.2.2/cc1plus /usr/tmp/cca11994.i -quiet -dumpbase testit.cc -version -o /usr/tmp/cca11994.s
GNU C++ version 2.2.2 (80386, ATT syntax) compiled by GNU C version 2.2.1.
 as -o testit.o /usr/tmp/cca11994.s
 ld /lib/crt1.o /usr/local/lib/gcc-lib/sco/2.2.2/crtbegin.o -L/usr/local/lib/gcc-lib/sco/2.2.2 -L/usr/local/lib testit.o -lgcc -lc -lgcc /usr/local/lib/gcc-lib/sco/2.2.2/crtend.o /lib/crtn.o
$ ./a.out
E's create
E's create
E's create
----------------------------------------------------------------------
extern "C" {
   void puts(const char *);
}

class Thing
{
   int t;
 public:
   Thing() {}
   virtual ~Thing() {}
   virtual void f() {}
};

class A
{
   Thing a;
 public:
   A() {}
   virtual ~A() {}
   virtual void isValid() {}
};

class Q
{
   Thing q;
 public:
   Q() {}
   virtual ~Q() {}
   virtual void create() { puts("Q's create"); }
};

class B : public Q
{
   Thing b;
 public:
   ~B() {}
};

class C : public A, public B
{
   Thing c;
 public:
   C();
   ~C() {}
   void isValid() {}
};

class D : public C
{
   Thing d;
 public:
   D();
   ~D() {}
   void create() { puts("D's create"); }
   void isValid() {}
};

class E : public C
{
   Thing e;
 public:
   E(); 
   ~E() {}
   void create() { puts("E's create"); }
   void isValid() {}
};

D::D() { }
E::E() { }
C::C() { }

C * f()
{
   static i = 0;
   i++;
   if (i==1) return new C;
   if (i==2) return new D;
   if (i==3) return new E;
   return 0;
}

main()
{
   f()->create();
   f()->create();
   f()->create();
}
Technical Details
Message-ID: <9210020955.aa12194@vega.srg.af.mil>
Path: arc1-ca.pugleaf.net!sparky!uunet!convex!darwin.sura.net!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!uunet.uu.net!enewton

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug
Path: arc1-ca.pugleaf.net!sparky!uunet!convex!darwin.sura.net!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!uunet.uu.net!enewton
From: enewton@uunet.uu.net
Subject: wrong virtual called in MI cases
Message-ID: <9210020955.aa12194@vega.srg.af.mil>
Sender: gnulists@ai.mit.edu
Reply-To: srg!enewton@uunet.uu.net
Organization: GNUs Not Usenet
Distribution: gnu
Date: Fri, 2 Oct 1992 13:55:46 GMT
Approved: bug-g++@prep.ai.mit.edu
Lines: 118