Re: private/protected
From: DaKoadMunky (dakoadmunky_at_aol.com)
Date: 04/29/04
- Next message: Gunnar G: "Re: Cubic Root"
- Previous message: MPBroida: "Re: Why won't the compiler tell??"
- In reply to: Chris Theis: "Re: private/protected"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Gunnar G: "Re: Cubic Root"
- Previous message: MPBroida: "Re: Why won't the compiler tell??"
- In reply to: Chris Theis: "Re: private/protected"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|