🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Dan DuVarney Tue, 11 Apr 1989 21:00
virtual function / private constructor bug
#1843
Author: 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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads