problem: private inheritance. How to explain it in terms of C++ rules?

M

Michal

hallo group members, I wonder if the example below is consistent with a
C++ rules:

#include <iostream>

using namespace std;

class A {
};

class B: private A {
};

int main(int argc, char *argv[])
{
B *b = new B;
A *a = (A*)b;
A *c = b; /* produces follwowing error:
p5.cpp: In function "int main(int, char**)":
p5.cpp:15: error: "A" is an inaccessible base of "B"

What is the reason for it???

*/
return 0 ;
}

best regards,
Michal
 
M

Marcel Müller

Michal said:
hallo group members, I wonder if the example below is consistent with a
C++ rules:

It is.
class A {
};

class B: private A {
};

int main(int argc, char *argv[])
{
B *b = new B;
A *a = (A*)b;
A *c = b; /* produces follwowing error:
p5.cpp: In function "int main(int, char**)":
p5.cpp:15: error: "A" is an inaccessible base of "B"

What is the reason for it???

Neither the implicit nor the explicit conversion to a private base class
is allowed outside the class. Otherwise, the base would no longer be
private.
Your cast to (A*) is a reinterpret cast and will badly fail in
conjunction with multiple inheritance, at least.


Marcel
 
A

Alf P. Steinbach

* Marcel Müller:
Michal said:
hallo group members, I wonder if the example below is consistent with a
C++ rules:

It is.
class A {
};

class B: private A {
};

int main(int argc, char *argv[])
{
B *b = new B;
A *a = (A*)b;
A *c = b; /* produces follwowing error:
p5.cpp: In function "int main(int, char**)":
p5.cpp:15: error: "A" is an inaccessible base of "B"

What is the reason for it???

Neither the implicit nor the explicit conversion to a private base class
is allowed outside the class. Otherwise, the base would no longer be
private.
Your cast to (A*) is a reinterpret cast

No, it's a very special case, §5.4/7, that a C style cast can convert to a
pointer or reference to an inaccessible but unambigious base class as if
static_cast was used for an accessible base (it can also do more such stuff).

So since the cast here would have been a static_cast if the base was accessible
(because static_cast comes before reinterpret_cast in the list of possibilities,
it goes const_cast, static_cast, static_cast+const_cast, reinterpret_cast,
reinterpret_cast+const_cast), it is therefore a static_cast.

And technically utterly safe, although -- as illustrated here -- not very
safe against being misunderstood or misidentified, even by the original author.


and will badly fail in
conjunction with multiple inheritance, at least.

Depends.


Cheers & hth.,

- Alf
 
M

Marcel Müller

Hi
No, it's a very special case, §5.4/7, that a C style cast can convert to
a pointer or reference to an inaccessible but unambigious base class as
if static_cast was used for an accessible base (it can also do more such
stuff).

uh, what an unexpected behavior. So there is no C++ style cast to do the
same job, is it?


Marcel
 
J

James Kanze

uh, what an unexpected behavior. So there is no C++ style cast
to do the same job, is it?

No.

I suspect that the incoherence is due to some reason of backward
compatibility, but I don't know enough about it to be sure.
Still, it's a conversion that you should never want to do, so
just use the C++ style casts, and forget about it.
 
A

Alf P. Steinbach

* Marcel Müller:
Hi


uh, what an unexpected behavior. So there is no C++ style cast to do the
same job, is it?

Nope.

One might think of it this way: accessing an inaccessible base class is
low-level and dirty, and so must be done using low-level and dirty notation.

The problem is that that notation is overloaded to the extreme, so that (1) it's
often practically impossible to predict what it'll do without delving into the
finer details of the standard, and (2) it's difficult or practically impossible
to search the code for that notation used for any particular purpose.


Cheers & hth.,

- Alf
 
M

Michal

James Kanze said:
I suspect that the incoherence is due to some reason of backward
compatibility, but I don't know enough about it to be sure.
Still, it's a conversion that you should never want to do, so
just use the C++ style casts, and forget about it.

James, Marcel thank You for the answers

best regards,
Michal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top