Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Private base class conversion from base?

Reply
Thread Tools

Private base class conversion from base?

 
 
jared.grubb@gmail.com
Guest
Posts: n/a
 
      09-11-2008
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;
}
 
Reply With Quote
 
 
 
 
Zeppe
Guest
Posts: n/a
 
      09-11-2008
Dear Jared,

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
 
Reply With Quote
 
 
 
 
JaredGrubb
Guest
Posts: n/a
 
      09-12-2008
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
<(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
Zeppe
Guest
Posts: n/a
 
      09-12-2008
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
 
Reply With Quote
 
Zeppe
Guest
Posts: n/a
 
      09-12-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why polymorph fails when virtual function is decleared private in base class and public in derived class? Fred C++ 2 06-03-2007 02:59 PM
Can a sub-class (composite object) member function access private or protected members of base class? pkpatil@gmail.com C++ 2 06-09-2006 03:09 PM
Access of base class' private base class: qualification required, why Alf P. Steinbach C++ 6 09-03-2005 04:03 PM
Public Data in Private Class or Private Data in Public Class? DaveLessnau C++ 3 05-16-2005 06:53 PM
Re: Copy assignment for derived class when base class has private members John Harrison C++ 0 08-06-2003 08:50 AM



Advertisments