Problem in private inheritance

Discussion in 'C++' started by Bangalore, Oct 3, 2005.

  1. Bangalore

    Bangalore Guest

    Hi,

    I am not understanding the problem in the below program,
    #include <iostrema.h>
    class Base
    {
    public:
    virtual void display()
    {
    cout <<"Base \n";
    }
    };
    class Derived:private Base
    {
    public:
    void display()
    {
    cout <<"Derived \n";
    }
    };
    int man()
    {
    Base *ptr=new Derived;

    ptr->display();
    return 0;
    }

    If I compile it, if flashes error "cannot convert Derived * to
    Base*"
    Can anybody clarify , why we get this error, that is only with
    private
    inheritance.

    Thanks,
    Bangalore
     
    Bangalore, Oct 3, 2005
    #1
    1. Advertising

  2. Bangalore

    FabioAng Guest

    "Bangalore" <-spam.invalid> wrote in
    message news:4340df34$0$81711$...
    > Hi,
    >
    > I am not understanding the problem in the below program,
    > #include <iostrema.h>
    > class Base
    > {
    > public:
    > virtual void display()
    > {
    > cout <<"Base \n";
    > }
    > };
    > class Derived:private Base
    > {
    > public:
    > void display()
    > {
    > cout <<"Derived \n";
    > }
    > };
    > int man()
    > {
    > Base *ptr=new Derived;
    >
    > ptr->display();
    > return 0;
    > }
    >
    > If I compile it, if flashes error "cannot convert Derived * to
    > Base*"
    > Can anybody clarify , why we get this error, that is only with
    > private
    > inheritance.
    >
    > Thanks,
    > Bangalore
    >

    Look at this:

    http://www.parashift.com/c -faq-lite/private-inheritance.html

    Bye
    Fabio
     
    FabioAng, Oct 3, 2005
    #2
    1. Advertising

  3. Bangalore

    Bangalore Guest

    Look at this:[/quote]

    http://www.parashift.com/c -faq-lite/private-inheritance.html

    Bye
    Fabio[/quote]

    Thanks Fabio.
    In that FAQ , I could not understand some portion that is quoted.
    Eventhough it has been clearly explained, but I am not understanding.
    can anybody clarify this with small example.

    24.4] Should I pointer-cast from a private derived class to its base
    class?

    Generally, No.

    > "--->From a member function or friend of a privately derived

    class, the relationship to the base class is known, and the upward
    conversion from PrivatelyDer* to Base* (or PrivatelyDer& to
    Base&) is safe; no cast is needed or recommended"..

    However users of PrivatelyDer should avoid this unsafe conversion,
    since it is based on a private decision of PrivatelyDer, and is
    subject to change without notice.
     
    Bangalore, Oct 3, 2005
    #3
  4. Bangalore

    Zorro Guest

    I read [24.4], and I will rephrase it. That does not mean I agree with
    it.

    Inside the definition of bodies of methods of class PrivatelyDer (and
    friends of it), you can do as you have done in main(), without casting.
    However, other functions such as main() are users of this class. Such
    users need explicit cast, like (Base*). It also says to avoid doing
    that, but I do not quite see the point in the warning stated there.

    I hope this helps. I know it is irritating. The general idea however
    lies in the private derivation which makes the Base private to the
    Derived class, and therefore not accessible. I would appreciate if
    someone more knowledgeable points to an item in the standard that
    explains this behavior, in the context of polymorphism.

    Regards,

    http://www.zhmicro.com
    http://distributed-software.blogspot.com
    http://groups-beta.google.com/group/computerlangZ?lnk=la&hl=en
     
    Zorro, Oct 3, 2005
    #4
  5. Bangalore

    Bangalore Guest

    Thanks a lot,
    Bangalore
     
    Bangalore, Oct 4, 2005
    #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. maxw_cc
    Replies:
    1
    Views:
    3,197
    Martijn van Steenbergen
    Dec 21, 2003
  2. qazmlp
    Replies:
    19
    Views:
    824
    Daniel T.
    Feb 4, 2004
  3. Mark A. Gibbs

    Private/protected inheritance problem

    Mark A. Gibbs, Mar 12, 2005, in forum: C++
    Replies:
    3
    Views:
    1,655
    Mark A. Gibbs
    Mar 12, 2005
  4. dragon
    Replies:
    5
    Views:
    398
    dragon
    Dec 22, 2005
  5. karthikbalaguru
    Replies:
    9
    Views:
    1,067
Loading...

Share This Page