Re: Am I missing something in private inheritance?

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 01/11/05


Date: Mon, 10 Jan 2005 19:01:01 -0500


"Alf P. Steinbach" <alfps@start.no> wrote...
>* Victor Bazarov:
>>
>> So, Alf, what's your take on it? I say, since the default c-tor of the
>> virtual base class is public, it should be accessible. Do you think
>> otherwise?
>
> In part I do. I disagree strongly with the justification, it does not
> make
> sense to me. Regarding the conclusion, for the specific case of default
> constructor, I do not know.
>
>
>> Could you give justification (from the Standard) why it would
>> not be accessible?
>
> Private inheritance.
>
> It gives non-accessibility for anything except the generated default
> constructor.
>
> For the generated default constructor, I do not know, but as earlier
> stated I strongly suspect a compiler bug in three compilers, namely
> Comeau, GNU
> and MSVC.
>
> For example, the following code does _not_ compile with MSVC, which is as
> it
> should be IMHO:
>
>
> class HiddenSealBaseClass
> {
> public:
> HiddenSealBaseClass() { }
> };
>
> class Sealed: virtual HiddenSealBaseClass
> {};
>
> class SomeClass: Sealed
> {
> SomeClass(): HiddenSealBaseClass() {}
> // Whatever
> };
>
> int main()
> {
> SomeClass obj;
> }
>
>
> vc_project.cpp(22): error C2248: 'SomeClass::SomeClass' : cannot access
> private
> member declared in class 'SomeClass'

This is where I am puzzled to the extreme. It seems that SomeClass has
less access to 'HiddenSealBaseClass' than anybody else. That goes against
_any_ possible role an access specifier is supposed to play.

Now, here is another example where it would be really bad.

I have two classes, B and D. B has a public default constructor (whether
it's generated or user-defined shouldn't matter). In D I have an object
of type B, which is created using all default ways (no mention of it in
the D's constructor initialiser list). Now, I decide to inherit [some
functionality] from a third-party class C, which at the time virtually and
publicly derived from B. My program is well-formed. Now, the third party
decides to change public inheritance to private. BAM! Suddenly, my
program's well-formedness depends on whether C is derived from B and how.
What?!! No, sorry, B's constructor is public and it should be accessible
_no_matter_ who and how intends to use it.

The main point of private inheritance is to prevent automatic conversions
from the derived to base. When the base class is used/instantiated without
any intervening derived members (and that's the case in virtual inheritance
among others), IOW, directly, the access used is the one that's specified
by the base class _itself_.

Victor



Relevant Pages

  • Re: copy contructor
    ... inheritance in Perl) it *has* an accessor. ... One of the tenets about Perl inheritance is "If you ... Sure it can have its own constructor. ... provided you call the base class' ...
    (comp.lang.perl.misc)
  • OOP question
    ... I get the jist of this Inheritance stuff, I think, I'm just looking to ... I created a MustInherit base class with a constructor that had arguments. ... PROTECTED because I reasoned that if I NEEDED to expose the module level ...
    (microsoft.public.dotnet.languages.vb)
  • Help: virtual inheritance, non-default constructor
    ... The base class "B" cannot be initialized because it does not have a default ... If I remove the "virtual" inheritance of B (so that it uses plain ... It also compiles if I invoke the ... constructor for B explicitly, as shown in the comment after the error. ...
    (comp.lang.cpp)
  • Re: Did I write a good (efficient) program?
    ... The priniples of object-oriented design do not include ... the use of inheritance to overcome inadequate or faulty ... modifying a base class may be ... principles to those of the paradigm. ...
    (comp.lang.cobol)
  • Re: Forms inheritance and templates
    ... inheritance correctly and avoiding having the delete controls. ... base form with a TabControl that has one tab that is common to all derived ... I suspect this is because the base class controls are created ...
    (microsoft.public.dotnet.framework.windowsforms)