473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Am I missing something in private inheritance?

May someone explain why does this compile?
class HiddenSealBaseClass
{
public:
HiddenSealBaseClass() { }
};

class Sealed: virtual HiddenSealBaseClass
{};

class SomeClass: Sealed
{
// Whatever
};


int main()
{
SomeClass obj;
}
Since we have *private* virtual inheritance, the public constructor
HiddenSealBaseClass() should have become private after Sealed
derivation, and thus inaccessible to SomeClass.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #1
10 2457
"Ioannis Vranos" <iv*@remove.this.grad.com> wrote...
May someone explain why does this compile?
class HiddenSealBaseClass
{
public:
HiddenSealBaseClass() { }
};

class Sealed: virtual HiddenSealBaseClass
{};

class SomeClass: Sealed
{
// Whatever
};


int main()
{
SomeClass obj;
}
Since we have *private* virtual inheritance, the public constructor
HiddenSealBaseClass() should have become private after Sealed derivation,
and thus inaccessible to SomeClass.


That's utter nonsense.

class A {
public:
void foo();
};

class B : A { // private inheritance
};

int main() {
A a;
a.foo(); // according to you it shouldn't compile because
// somebody else inherits from A privately
}

Victor
Jul 22 '05 #2
* Victor Bazarov:
"Ioannis Vranos" <iv*@remove.this.grad.com> wrote...
May someone explain why does this compile?
class HiddenSealBaseClass
{
public:
HiddenSealBaseClass() { }
};

class Sealed: virtual HiddenSealBaseClass
{};

class SomeClass: Sealed
{
// Whatever
};


int main()
{
SomeClass obj;
}
Since we have *private* virtual inheritance, the public constructor
HiddenSealBaseClass() should have become private after Sealed derivation,
and thus inaccessible to SomeClass.


That's utter nonsense.

class A {
public:
void foo();
};

class B : A { // private inheritance
};

int main() {
A a;
a.foo(); // according to you it shouldn't compile because
// somebody else inherits from A privately
}


The problem is more subtle than that, Victor, in your example
somewhat analogous to

class C: public B
{
void victorLookHere(){ foo(); }
};

What might make the analogy break down is that this is about a
default constructor and virtual inheritance, not ordinary
inheritance and an ordinary member function or constructor.

The reason there is doubt is that at least three modern compilers
don't complain.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3
Victor Bazarov wrote:
That's utter nonsense.

class A {
public:
void foo();
};

class B : A { // private inheritance
};

int main() {
A a;
a.foo(); // according to you it shouldn't compile because
// somebody else inherits from A privately
}



Nope. You are using two classes while I am using three. Making your
example more equivalent:

class A {
public:
void foo();
};

class B : A {
};
class C: B {};

int main() {
C a;
a.foo();
}
This does not compile.

or even more equivalent:

class A {
public:
void foo();
};

class B : virtual A {
};
class C: B {};

int main() {
C a;
a.foo();
}

The above does not compile too, however it does compile if we use a
public A() constructor.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4
Alf P. Steinbach wrote:
* Victor Bazarov:
"Ioannis Vranos" <iv*@remove.this.grad.com> wrote...
May someone explain why does this compile?
class HiddenSealBaseClass
{
public:
HiddenSealBaseClass() { }
};

class Sealed: virtual HiddenSealBaseClass
{};

class SomeClass: Sealed
{
// Whatever
};


int main()
{
SomeClass obj;
}
Since we have *private* virtual inheritance, the public constructor
HiddenSealBaseClass() should have become private after Sealed derivation,
and thus inaccessible to SomeClass.
That's utter nonsense.

class A {
public:
void foo();
};

class B : A { // private inheritance
};

int main() {
A a;
a.foo(); // according to you it shouldn't compile because
// somebody else inherits from A privately
}

The problem is more subtle than that, Victor, in your example
somewhat analogous to

class C: public B
{
void victorLookHere(){ foo(); }


So, we're saying that in this case 'C' shouldn't know that it's derived
from 'A' (as well) since 'A' is a private base of 'B', right? IOW, to
anybody outside 'B' B::foo() does not exist, right? Actually I agree.

So, in 'C' scope, 'foo' shouldn't be resolved to A::foo because 'C' must
not know about 'A' being one of its base classes. Perfectly fine.
};

What might make the analogy break down is that this is about a
default constructor and virtual inheritance, not ordinary
inheritance and an ordinary member function or constructor.
Yes. The need to construct a virtual base class object in the most
derived class' constructor makes the whole thing tricky.

The reason there is doubt is that at least three modern compilers
don't complain.


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? Could you give justification (from the Standard) why it would
not be accessible? While you're at it, look at 12.6.2/6. "If V does not
have an accessible default constructor, the initialisation is ill-formed".
IOW, if it does [have an accessible default constructor], it would be OK.

Thanks.

V
Jul 22 '05 #5
Ioannis Vranos wrote:
Victor Bazarov wrote:
That's utter nonsense.

class A {
public:
void foo();
};

class B : A { // private inheritance
};

int main() {
A a;
a.foo(); // according to you it shouldn't compile because
// somebody else inherits from A privately
}


Nope. You are using two classes while I am using three. Making your
example more equivalent:

class A {
public:
void foo();
};

class B : A {
};
class C: B {};

int main() {
C a;
a.foo();
}
This does not compile.


Of course it doesn't. You're treating 'foo' as if it's a member of 'C'.
Your original post required access to A::A(), which was never private to
begin with.
or even more equivalent:

class A {
public:
void foo();
};

class B : virtual A {
};
class C: B {};

int main() {
C a;
a.foo();
}

The above does not compile too, however it does compile if we use a
public A() constructor.


Again. C::foo is private. A::A is not.

Victor
Jul 22 '05 #6
* 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'

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #7
"Alf P. Steinbach" <al***@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
Jul 22 '05 #8
Victor Bazarov wrote:
or even more equivalent:

class A {
public:
void foo();
};

class B : virtual A {
};
class C: B {};

int main() {
C a;
a.foo();
}

The above does not compile too, however it does compile if we use a
public A() constructor.

Again. C::foo is private. A::A is not.

Yes, however, why this does not happen in the case of a public default
constructor?

It looks like the type of inheritance (private, protected, public), does
not affect inherited public/protected constructors, but only inherited
public/protected data members and member functions.

Is this right?


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #9
"Ioannis Vranos" <iv*@remove.this.grad.com> wrote...
[..]
It looks like the type of inheritance (private, protected, public), does
not affect inherited public/protected constructors, but only inherited
public/protected data members and member functions.

Is this right?


Constructors are not inherited, perhaps that's the explanation you're
looking for.

Victor
Jul 22 '05 #10
Victor Bazarov wrote:
Constructors are not inherited, perhaps that's the explanation you're
looking for.

Yes, that's it I guess.
However (*this is another case*) strangely enough, this code from the
FAQ works:
class Fred;

class FredBase {
private:
friend class Fred;
FredBase() { }
};

class Fred : private virtual FredBase {
public:
...
};

That is, although FredBase() constructor is *private*, Fred being a
friend of FredBase "has access" to this constructor.
So in summary, constructors are not inherited, however a class being a
friend can access private constructors as happens with usual private
methods.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error...
2
by: MJ | last post by:
Hi I have a following sample code class base and class derived. I have inherited the base class as private and tried to compile the code its giving an error "conversion from 'class derived *' to...
1
by: Tony Johansson | last post by:
Hello! Private inheritance is sometimes called implementation inheritance. If you use this private inheritance how is with the usage of overriding then. Is overriding used less often when...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
6
by: karthikbalaguru | last post by:
Hi, Could someone here tell me some links/pdfs/tutorials to know about the difference between Private Inheritance and Public Inheritance ? I am unable to get info w.r.t it. Thx in advans,...
8
by: puzzlecracker | last post by:
The statement is taken from FAQ . What about non-virtual functions? Can they be overriden? I still don't see a good justification to prefer private inheritance over composition. In fact, I have...
28
by: jmDesktop | last post by:
Studying OOP and noticed that Python does not have Interfaces. Is that correct? Is my schooling for nought on these OOP concepts if I use Python. Am I losing something if I don't use the...
13
by: PragueExpat | last post by:
I (think) that I've come up with a pattern that I haven't seen in any publications so far and I would like some feedback. Basically, I was looking for a way to inherit private functions and I came...
4
by: zhangyefei.yefei | last post by:
i read book <effective c++>,it tell me that public inheritance means is-a ,and private inheritance means is-implemented-in-terms-of. but today i am puzzled by some strange codes. the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.