Problem with virtual inheritance

Jakob Østergaard jakob@ostenfeld.dk
Mon Nov 1 07:36:00 GMT 1999


Hi all !

I *may* have found a bug in G++ 2.95.1, and from what I could
read it hasn't been addressed in 2.95.2.  Platform is RedHat Linux
6.0 on i686.

I cannot read from the standard (The C++ programming language by Bjarne
Stroustrup, third edition) whether it's my code that's wrong, or the
compiler.

The following program prints "Baddie" not "Hello" as I would have
expected:

---------------------8<---------8<-----------------
#include <string>
#include <iostream>

class stderr {
public:
  stderr(const string& e) : err(e) {}
  stderr() : err("Baddie!") {}
  operator string() const { return err; }
private:
  string err;
};

class stdinh : public virtual stderr {
public:
  stdinh(const string& e) : stderr(e) {}
};

class deriv : public stdinh {
public:
  deriv(const string& e) : stdinh(e) {}
};

void call_func()
{
  throw deriv("Hello!");
}

int main()
{
  try {
    call_func();
  } catch(stdinh e) {
    cerr << string(e) << endl;
  }
}
---------------------8<---------8<-----------------

If I remove the ``virtual''  keyword, the instantiation of
a deriv object will call the  stderr(const string&) constructor
of stderr, instead of the stderr() constructor.

Is this a compiler bug ?   Or is it perfectly correct of the
compiler to ignore the 
        stdinh(const string& e) : stderr(e) {}
constructor and just call stderr() instead ?

I know that there's no point in using virtual inheritance in the
above code snippet. But still the compiler should behave correctly.
If anyone could tell me whether it's me or the compiler that doesn't
know the standard, I'd appreciate it very much.  

Thank you very much,
-- 
................................................................
: jakob@ostenfeld.dtu.dk  : And I see the elder races,         :
:.........................: putrid forms of man                :
:   Jakob Østergaard      : See him rise and claim the earth,  :
:        OZ9ABN           : his downfall is at hand.           :
:.........................:............{Konkhra}...............:


More information about the Gcc-bugs mailing list