Alf P. Steinbach said:
* Victor Bazarov:
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.
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