Topic: private virtual inheritance


Author: dtmoore@email.unc.edu ("Dave Moore")
Date: Fri, 21 Jan 2005 04:31:32 GMT
Raw View
I asked a modified version of the following question in c.l.c++.mod
recently, and I haven't gotten a response ... perhaps this is a more
appropriate NG?

Consider the following class example:

#include <iostream>


class I {
  char _c;
public:
  I() : _c('i') {}
  I(char c) : _c(c){}
  virtual void print() const { std::cout << _c << std::endl;}
};

class A : private virtual I {
public:
  // A() : I('a') {}
  virtual void print() const { I::print();}
};

class B : private virtual I {
public:
  // B() : I('b') {}
};


class C : public A, public B {
public:
  C() : I('c') {}
};

int main()
{
  C c;
  c.print();
}

I tested this code with gcc 3.3.1 and Comeau online 4.3.3, and neither of
them behaved exactly as I expected.  As is, the code compiles successfully
with g++ but fails with Comeau, which says (as I expected) that the
I::I(char) ctor is inaccessible from class C.  If I comment out the ctor for
class C, everything works fine on both compilers.  If I explicitly use I()
instead of I('c') as a mem-initializer in the class C ctor, everything works
fine on both compilers.  How are the latter two cases different from the
first regarding the accessibility of the class I ctors?

Also, how *should* this be handled by a Standard conforming
compiler?  The Standard says (12.6.2 /6) that only the most derived class is
responsible for instantiating class I, however class I is not an accessible
base class of C, so its constructors should not be accessible to the
most-derived class C.  I checked around in clauses 10-12 of the standard for
caveats that would allow the behavior of Comeau and or g++, but I couldn't
find any.  Am I missing something?

As sort of a side question, is virtual private inheritance useful for
anything?  I searched the C++ NG's and googled for it, but I couldn't find
any particularly useful or convincing examples.

TIA,

Dave Moore


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: rlankine@hotmail.com ("Risto Lankinen")
Date: Fri, 21 Jan 2005 17:23:07 GMT
Raw View
""Dave Moore"" <dtmoore@email.unc.edu> wrote in message
news:35510sF4i6g1qU1@individual.net...
>
> As sort of a side question, is virtual private inheritance useful for
> anything?

I myself don't know the definite answer to your real question, but
my gut feeling is that Comeau is right.  If that is indeed true, then I
can think of at least one use for virtual private inheritance:  Seemingly
any class could then be made "final" (i.e. uninheritable) by giving it
a virtual base class having no default constructor.

 - Risto -

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]