Re: private/protected

From: DaKoadMunky (dakoadmunky_at_aol.com)
Date: 04/29/04


Date: 29 Apr 2004 20:31:53 GMT


>public inheritance represents "is-a" relationship, whereas this is not the
>case with private inheritance. This means that a class B which is privately
>derived from A is NOT an object of type A and hence cannot be converted to
>one (at least not by the compiler).

The "is-a" relationship does not exist for public users of the derived type,
but it does exist within the derived type and is available to friends of the
derived type who are then freely able to convert between the derived type and
its private base.

A popular C++ author describes this as "controlled polymorphism."

struct Base
{
};

class Derived : private Base
{
        friend void FriendOfDerived();
};

void FriendOfDerived()
{
        Derived d;
        Base *bptr = &d; //Okay! Conversion available here!
}

int main()
{
        Derived d;
        Base *bptr = &d; //Error! Conversion not available here!

              return 0;
}

Is this "controlled polymorphism" actually used in real-word designs?

I would be curious to know.



Relevant Pages

  • Re: How to... private inheritance ?!
    ... implementation of the methods exposed by the interface. ... to access the interface's methods through the concrete class itself. ... but my recollections on private inheritance is designed for do not match your description. ...
    (comp.lang.java.programmer)
  • Re: basic question on C++
    ... |>On a job interview question, ... | All I can think of is that "how do you set variables in the base class ... | to be all private" is meant as if it were followed with "...to the ... That would be via private inheritance: ...
    (alt.comp.lang.learn.c-cpp)
  • RE: UML relation for C++ private inheritance
    ... You can set the stereotype on the generalization relationship between your ... your model and selecting private from the Stereotype dropdown list. ... UML relation for C++ private inheritance ... At the moment I am building a UML static structure diagram. ...
    (microsoft.public.visio.software.modeling)