What's "private inheritence" for?

This is a discussion on What's "private inheritence" for? within the C++ Programming forums, part of the General Programming Boards category; It seems that Derived class still can access Base::z? Code: class Base{ public: int z; Base(){ cout<<"Base::Base()"<<endl; } ~Base(){} }; ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    What's "private inheritence" for?

    It seems that Derived class still can access Base::z?
    Code:
    class Base{
    public:
        int z;
        Base(){
            cout<<"Base::Base()"<<endl;
        }
        ~Base(){}
    };
    
    class Derived : private Base{
    
    public:
        Derived():m1(){
            cout<<"Derived::Derived()"<<endl;
            z=3;
        }
    };
    
    int main(){
        Derived de;
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Of course Derived can, but what about:

    Example:
    Code:
    class DerviedFromDerived : public Derived
    {
    public:
      DerivedFromDerived()
      {
    
      }
    };
    What happens when you try to use something from a base from this context?

  3. #3
    Beginner leiming's Avatar
    Join Date
    Jan 2008
    Location
    Fujian, China
    Posts
    25
    z is private for derived, so it can access z.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Generally, you will want to use private inheritance to inherit from a class whose contents is not to be part of the public interface (ie what the user of the class can use).
    This is because everything inherited, regardless if it's public, protected or private, will become private in the derived class.
    You can also use protected inheritance which can be useful if you do not want the contents to be part of the public interface, but make sure the class can be derived from. In protected inheritance, everything but private becomes protected.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    In protected inheritance, everything but private becomes protected.
    Are you sure?
    I'm pretty sure visibility only becomes more restrictive, not less. i.e. private stays private, public becomes protected...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by cpjust View Post
    Are you sure?
    I'm pretty sure visibility only becomes more restrictive, not less. i.e. private stays private, public becomes protected...
    Isn't that what Elysia said?

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by tabstop View Post
    Isn't that what Elysia said?
    Damn, nevermind. I didn't see the "but" in there.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to access "private or protected " in Class
    By johnnypiere in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2002, 01:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21