🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

virtual function / private constructor bug

#1843
From: Dan DuVarney
Date: Tue, 11 Apr 1989 21:00
74 lines
1982 bytes
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
Technical Details
Message-ID: <5064@xyzzy.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!rutgers!labrea!mcnc!rti!xyzzy!bert!duvarney

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!rutgers!labrea!mcnc!rti!xyzzy!bert!duvarney
From: duvarney@bert.dg.com (Dan DuVarney)
Newsgroups: gnu.g++.bug
Subject: virtual function / private constructor bug
Message-ID: <5064@xyzzy.UUCP>
Date: 11 Apr 89 21:00:49 GMT
Sender: usenet@xyzzy.UUCP
Reply-To: duvarney@dg-rtp.dg.com ()
Organization: Data General Corporation, Research Triangle Park, NC
Lines: 73