By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 1,966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

Private base class conversion from base?

P: n/a
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;
}
Sep 11 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

On Sep 11, 9:02*am, Zeppe
<ze...@remove.all.this.long.comment.yahoo.itwrot e:
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.
Sep 12 '08 #2

P: n/a
JaredGrubb wrote:
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
Sep 12 '08 #3

P: n/a
JaredGrubb wrote:
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
Sep 12 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.