Private base class conversion from base?

Discussion in 'C++' started by jared.grubb@gmail.com, Sep 11, 2008.

  1. Guest

    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, 2008
    #1
    1. Advertising

  2. Zeppe Guest

    Dear Jared,

    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
    Zeppe, Sep 11, 2008
    #2
    1. Advertising

  3. JaredGrubb Guest

    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
    <> 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.
    JaredGrubb, Sep 12, 2008
    #3
  4. Zeppe Guest

    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
    Zeppe, Sep 12, 2008
    #4
  5. Zeppe Guest

    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
    Zeppe, Sep 12, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Harrison
    Replies:
    0
    Views:
    482
    John Harrison
    Aug 6, 2003
  2. DaveLessnau
    Replies:
    3
    Views:
    413
    Howard
    May 16, 2005
  3. Alf P. Steinbach
    Replies:
    6
    Views:
    531
    John Carson
    Sep 3, 2005
  4. Replies:
    2
    Views:
    796
    Noah Roberts
    Jun 9, 2006
  5. , India
    Replies:
    8
    Views:
    932
    gwowen
    Aug 18, 2010
Loading...

Share This Page