POV-Ray : Newsgroups : povray.programming : C++ inheritance puzzler : Re: C++ inheritance puzzler Server Time
29 Apr 2024 08:22:32 EDT (-0400)
  Re: C++ inheritance puzzler  
From: Warp
Date: 29 Nov 2010 12:23:32
Message: <4cf3e194@news.povray.org>
clipka <ano### [at] anonymousorg> wrote:
> Why should (2) be prohibited when (1) is not? After all, the practical 
> use of protected members is to hide implementation details from "alien" 
> code; but the code in (2) is not alien - it must rely on that 
> implementation detail called "a()" even if it just calls it on itself, 
> so why prevent it from calling it on some other object of type A?

  I'd say you can think of it like when B inherits A, B inherits the
protected section of A, and it thus becomes also B's own protected section.
B can access its own protected section, but not A's protected section.
The distinction comes into play when you have an object of type B vs. an
object of type A. B can access the protected section of the former (because
it is part of B's protected section), but not the protected section of the
latter (because now it's purely A's protected section, not B's).

  (I'm not saying that this is a perfect design, or that it makes perfect
sense. I'm just describing the situation.)

  If you know that the object behind the pointer is really of type B, you
can make a cast to B. If the object has RTTI you can make a dynamic cast
to test if the object is of type B. If it's not of type B, then you just
can't call its protected section.

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.