Base class pointers and private inheritance

Discussion in 'C++' started by Dave Theese, Aug 23, 2003.

  1. Dave Theese

    Dave Theese Guest

    Hello all,

    The example below demonstrates proper conformance to the C++ standard.
    However, I'm having a hard time getting my brain around which language rules
    make this proper...

    The error below *should* happen, but my question to the community is *why*
    does it happen? Any answer will be appreciated, but a section and paragraph
    number from the C++ Standard would be especially appreciated.

    Thanks to all!
    Dave

    P.S. I do understand the conceptual difference between public / private
    inheritance (i.e. "is-a" vs. "has-a").

    struct b {};
    struct d: private b {};

    int main()
    {
    // By itself, "new d" is fine.
    new d;

    // This, however, yields the following error:
    // conversion from d* to b* exists but is inaccessible
    b *ptr = new d;
    }
    Dave Theese, Aug 23, 2003
    #1
    1. Advertising

  2. Dave Theese wrote:
    > Hello all,
    >
    > The example below demonstrates proper conformance to the C++ standard.
    > However, I'm having a hard time getting my brain around which language rules
    > make this proper...
    >
    > The error below *should* happen, but my question to the community is *why*
    > does it happen? Any answer will be appreciated, but a section and paragraph
    > number from the C++ Standard would be especially appreciated.
    >
    > Thanks to all!
    > Dave
    >
    > P.S. I do understand the conceptual difference between public / private
    > inheritance (i.e. "is-a" vs. "has-a").
    >
    > struct b {};
    > struct d: private b {};


    What you have said here in English:

    A "d" IS-A "b" but don't let anything but "d" use me as a "b".

    >
    > int main()
    > {
    > // By itself, "new d" is fine.
    > new d;
    >
    > // This, however, yields the following error:
    > // conversion from d* to b* exists but is inaccessible
    > b *ptr = new d;


    Here you try to use a "d" as a "b" in direct violation of what you said
    above.

    So what's the problem ?
    Gianni Mariani, Aug 23, 2003
    #2
    1. Advertising

  3. Dave Theese

    White Wolf Guest

    Dave Theese wrote:
    > Hello all,
    >
    > The example below demonstrates proper conformance to the C++ standard.
    > However, I'm having a hard time getting my brain around which
    > language rules make this proper...
    >
    > The error below *should* happen, but my question to the community is
    > *why* does it happen? Any answer will be appreciated, but a section
    > and paragraph number from the C++ Standard would be especially
    > appreciated.

    [SNIP]

    In 4.10 Pointer Conversions, Paragraph 3 Simon says:

    "An rvalue of type "pointer to cv D," where D is a class type, can be
    converted to an rvalue of type "pointer to cv B," where B is a base class
    (clause 10) of D. If B is an inaccessible (clause 11) or ambiguous (10.2)
    base class of D, a program that necessitates this conversion is illformed."

    --
    WW aka Attila
    White Wolf, Aug 23, 2003
    #3
  4. Dave Theese wrote:

    > Hello all,
    >
    > The example below demonstrates proper conformance to the C++ standard.
    > However, I'm having a hard time getting my brain around which language rules
    > make this proper...
    >
    > The error below *should* happen, but my question to the community is *why*
    > does it happen? Any answer will be appreciated, but a section and paragraph
    > number from the C++ Standard would be especially appreciated.
    >
    > Thanks to all!
    > Dave
    >
    > P.S. I do understand the conceptual difference between public / private
    > inheritance (i.e. "is-a" vs. "has-a").


    I don't believe such a conceptual difference exists. Inheritance always
    models "is a" relationships, in my opinion. The only difference is, who
    gets to know about the relationship and exploit it? In public
    inheritance, everyone can. In private inheritance, only members and
    friends can.

    >
    > struct b {};
    > struct d: private b {};
    >
    > int main()
    > {
    > // By itself, "new d" is fine.
    > new d;


    This isn't really "fine"... it's a memory leak.

    >
    > // This, however, yields the following error:
    > // conversion from d* to b* exists but is inaccessible
    > b *ptr = new d;


    Indeed. Function main() is not a member or friend of d, therefore may
    not do this conversion.

    Also, though it's not strictly necessary, it's a good idea to have a
    return statement here:

    return 0;

    > }
    >
    >


    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Aug 24, 2003
    #4
  5. Dave Theese

    Jerry Coffin Guest

    In article <WtQ1b.7710$QT5.2690@fed1read02>,
    says...
    > Hello all,
    >
    > The example below demonstrates proper conformance to the C++ standard.
    > However, I'm having a hard time getting my brain around which language rules
    > make this proper...
    >
    > The error below *should* happen, but my question to the community is *why*
    > does it happen?


    Because you've asked for it to happen. Private inheritance means
    exactly that: the relationship between the derived and the base class is
    not visible to the outside world, so the conversion from derived to base
    that's allowed with public inheritance isn't allowed with private
    inheritance.

    > P.S. I do understand the conceptual difference between public / private
    > inheritance (i.e. "is-a" vs. "has-a").


    A "has-a" relationship is normally expressed by aggregation -- i.e. one
    object containing an instance of another. The usual description for
    private inheritance is "is implemented in terms of". E.g. a stack being
    implemented in terms of a vector -- a stack doesn't support the full
    interface of a vector, so we can't use public inheritance. At the same
    time, it would be wasteful to use aggregation, because all the data
    needed for a stack is already contained in a vector.

    This is fairly typical: private inheritance is often used when the base
    object includes _more_ functionality than needed, and the private
    inheritance is used to create a more limited interface (directly
    contrary to public inheritance, which means the derived object must
    support at least the full base interface, and may add more).

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Aug 24, 2003
    #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. qazmlp
    Replies:
    19
    Views:
    779
    Daniel T.
    Feb 4, 2004
  2. DaveLessnau
    Replies:
    3
    Views:
    417
    Howard
    May 16, 2005
  3. Alf P. Steinbach
    Replies:
    6
    Views:
    534
    John Carson
    Sep 3, 2005
  4. Replies:
    4
    Views:
    467
    Zeppe
    Sep 12, 2008
  5. nguillot
    Replies:
    5
    Views:
    688
    itaj sherman
    Feb 24, 2011
Loading...

Share This Page