[comp.lang.c++] Derived* -> Base* conversion under public/private MI

sdm@cs.brown.edu (Scott Meyers) (02/12/91)

If a derived class inherits from a base class both publicly and privately,
should there be implicit conversions from a derived pointer to a base
pointer?

Example:

    class Base {};
    class Derived1: public virtual Base {};
    class Derived2: public virtual Base, private Derived1{};

    main()
    {
      Base *pb = new Derived2;   // legal?
    }

The assignment calls for an implicit conversion of a Derived2* to a Base*.
AT&T 2.0 says no can do, as does Sun 2.1beta.  As usual, g++ has to be
different, so it says all is well.

The ARM says (p. 36) 

     A pointer to a class may be converted to a pointer to an accessible
     base class of that class provided the conversion is unambiguous;  a
     base class is accessible if its public members are accessible.

This doesn't help much.  In the above example, Base's public members are
accessible through one path, inaccessible through the other.  Because Base
is a virtual base class, ambiguity is not a problem here.

I seem to recall reading somewhere that if a class inherits from another
class both publicly and privately, the public path wins, but I can't find
that in Lippman, Dewhurst & Stark, or the ARM.

An authoritative answer would be *very* helpful.

Thanks,

Scott






-------------------------------------------------------------------------------
What do you say to a convicted felon in Providence?  "Hello, Mr. Mayor."

peter@micromuse.co.uk (Peter Galbavy) (02/13/91)

In article <64530@brunix.UUCP> sdm@cs.brown.edu (Scott Meyers) writes:
>
>The ARM says (p. 36) 
>
>     A pointer to a class may be converted to a pointer to an accessible
>     base class of that class provided the conversion is unambiguous;  a
>     base class is accessible if its public members are accessible.
>
>This doesn't help much.  In the above example, Base's public members are
>accessible through one path, inaccessible through the other.  Because Base
>is a virtual base class, ambiguity is not a problem here.
>

I hope I am getting this right - but also the ARM says on page 117 (in
the UK at least - page 36 matches):

---- BOT ----
11.7 Multiple Access

If a name can be reached by several paths through a multiple
inheritance graph, the access is that of the path that gives most
access. For example,

	class W { public: void f(); };
	class A : private virtual W { };
	class B : public virtual W { };
	class C : public A, public B {
		void f() { W::f(); }	// ok
	};

Since W::f() is available to C::f() along the public path through B,
access is legal.
---- EOT ---- All typos are mine ----

I hope that is the right answer to the right question :-) I am still
VERY new to C++ (as far as I am concerned anyway).

On another point - can anyone recommmend a good (up to date) book
along the lines of C++ for C hackers ? I do not want C++ from first
principals, what I need is a book that will enlighten me in certain
areas, and leave my misconceptions alone in others :->

Regards,
-- 
Peter Galbavy

Writing at Home, but working for Micromuse Ltd, who don't even know I
am posting, and probably don't care, so this ain't nothing to do with
them... But if you must, call me daytime on +44 71 352 7774. **BEEP**