pointer and dynamic binding

Hi there,

Does a pointer to the base class point to the derived class that privately derives from base class?

Say:

class Base;
class private_derived : private Base;

private_derived PD;
Base *pb = &PD;

does it work for the definition of the pointer "pb"? why?
As long as you have authorization to access Base, then yes. Otherwise, no.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Base
{
};

class Derived : private Base
{
  void foo()
  {
    Derived d;
    Base* p = &d;  // OK to do here, because we're in Derived
      // and Derived has access to Derived's Base
      //  (Derived can access private stuff in Derived)
  }
};

int main ()
{
    Derived d;
    Base* p = &d;  // illegal here (compiler error)
        // because main cannot access private stuff in Derived
        // and Base is private in Derived
    return 0;
}
very interesting!

so this literally is private inheritance

inside Derived, you know that you are derived from Base (Line 10)
outside Derived, you cannot see this relationship (Line 19)

thx for the example, Disch

is there any advantage to hiding the inheritance relationship from the outside?
I personally have never had any situation where I found protected or private inheritance to be useful.
thanks for the example. Your explanation is cool. coz Base is private in Derived, so the Base pointer p has no access to the private component of Derived, just as any other private member in Derived, if it has any.

:)
@ Disch: Security tokens for Access Control Lists... That's all I've got, and yeah I know this is an oddly specific example but it's a task that is perfect for private\protected inheritance and polymorphism in general.
Topic archived. No new replies allowed.