Private base class conversion from base?

J

jared.grubb

Can a private Base class method convert to/from a Derived* using
static_cast? The CPL book says that a Derived method is allowed to
convert to/from Base, but says nothing about whether a Base method may
do the same thing.

My embedded compiler and g++ disagree on whether this is legal.
Example:

class Base { public: void foo(); };
class Derived : private Base { public: void foo(); };

void Base::foo() {
static_cast<Base*>((Derived*)0); // ok in g++, but legal?
static_cast<Derived*>((Base*)0); // ok in g++, but legal?
}
void Derived::foo() {
static_cast<Base*>((Derived*)0); // legal
static_cast<Derived*>((Base*)0); // legal
}

int main()
{
static_cast<Base*>((Derived*)0); // illegal
static_cast<Derived*>((Base*)0); // illegal
return 0;
}
 
Z

Zeppe

Dear Jared,

Can a private Base class method convert to/from a Derived* using
static_cast? The CPL book says that a Derived method is allowed to
convert to/from Base, but says nothing about whether a Base method may
do the same thing.

I would say that it is not allowed. In order for the static_cast to be
legal, as I see it, the base class has to be accessible, that is, a
public member of the base class can be invoked. Now,

static_cast<Derived*>((Base*)0);

is legal if

static_cast<Base*>((Derived*)0);

is. For the last statement, since Base is a private base of Derived, a
public member in Base is private in Derived, and a private member in
Derived can be accessed only from Derived (and friends). So, the class
Base is accessible only from Derived (and friends), that is the only
place in which such a cast is correct.

BTW, after writing this I tried your code with g++ (3.4, 4.0, 4.1, 4.2)
and it raises error when you say "ok with g++". So I don't know why with
you it worked.

Best wishes,

Zeppe
 
J

JaredGrubb

I'm sorry; in my tests at home I used PROTECTED inheritance, and in my
post I used PRIVATE. If you change to protected inheritance, then the
"ok with g++" does compile (at least on 4.0.1 and 4.1.2 I tried)
However, my embedded compiler complains for both protected & private
inheritance.

Which way is correct, or is it one of those "undefined" areas?

Jared
 
Z

Zeppe

JaredGrubb said:
I'm sorry; in my tests at home I used PROTECTED inheritance, and in my
post I used PRIVATE. If you change to protected inheritance, then the
"ok with g++" does compile (at least on 4.0.1 and 4.1.2 I tried)
However, my embedded compiler complains for both protected & private
inheritance.

Which way is correct, or is it one of those "undefined" areas?


If you consider protected inheritance, the thing works like that: all
the public and protected members of B are protected in D. Protected
members in D can be accessed by D and derived classes. B is not derived
from D, so again B is not a visible base of D in a B method and g++
should be wrong in this case.

Best wishes,

Zeppe
 
Z

Zeppe

JaredGrubb said:
I'm sorry; in my tests at home I used PROTECTED inheritance, and in my
post I used PRIVATE. If you change to protected inheritance, then the
"ok with g++" does compile (at least on 4.0.1 and 4.1.2 I tried)
However, my embedded compiler complains for both protected & private
inheritance.

Which way is correct, or is it one of those "undefined" areas?


If you consider protected inheritance, the thing works like that: all
the public and protected members of B are protected in D. Protected
members in D can be accessed by D and derived classes. B is not derived
from D, so again B is not a visible base of D in a B method and g++
should be wrong in this case.

Best wishes,

Zeppe
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top