[comp.lang.c++] Can a virtual base class have a non-virtual base ?

pjg@daedalus.osf.org (Paulo Guedes) (06/06/91)

Is it legal for a class that is used as a virtual base to have itself
a non-virtual base ? The following program ilustrates the problem:

class X {
      public:
	virtual int f1(char *m) { printf ("X::f1\n"); }
	virtual int do_something(char* m) { }
};

// Should X be a virtual base class ?
class X1 : public /* virtual */ X {
      public:
	virtual int do_something(char* m) { f1(m); }
};

class R: public virtual X1 {
      public:
	virtual int f1(char* m) { printf ("R::f1\n"); }
};

class S: public virtual X1 {
      public:
};

class T: public R, public S {
      public:
};


main()
{
	X* r = new R;
	r->do_something("Hi");
	X1* r1 = new R;
	r1->do_something("Hi");

	X1* x1 = new T;
	x1->do_something("Hi");
	T* t = new T;
	t->do_something("Hi");

}

g++-1.37.1 gives the following output:

a) With  'class X1 : public X {'
R::f1
R::f1
X::f1
X::f1

b) With  'class X1 : public virtual X {'
R::f1
R::f1
R::f1
R::f1

I expected both cases to produce output b). Is this a g++ bug ?
I don't have any other compiler available to check what they would
produce, and I couldn't find anything in E&S that clarifies this
situation. 

Thanks
Paulo

--
---
Paulo Guedes                 Email: pjg@osf.osf.org    Phone: (617) 621-8878
OSF Research Institute       11 Cambridge Center, Cambridge, MA 02142

marc@dumbcat.sf.ca.us (Marco S Hyman) (06/15/91)

In article <PJG.91Jun5180002@daedalus.osf.org> pjg@daedalus.osf.org (Paulo Guedes) writes:
 > Is it legal for a class that is used as a virtual base to have itself
 > a non-virtual base ? The following program ilustrates the problem:

Yes.  At least your example is accepted by a cfront 2.0 derived compiler and
gave the results you expected (all four printfs are "R::f1").  I think the
output you saw was a g++ bug.

// marc
-- 
// home: marc@dumbcat.sf.ca.us		pacbell!dumbcat!marc
// work: marc@ascend.com		uunet!aria!marc