0

I am learning the concepts of OOPS, and came across private inheritance. From what I've learnt - When a class derives from a base class and letter when the derived class is instantiated, the constructor of the Base class is called first, followed by the constructor of the derived class. So, in the code "A created" would be printed first. The problem is since the inheritance is private, all the members of A would be private inside B, so how can the constructor of A be called when B is instantiated. Going by this logic, the following code should generate errors, but when I run it, it compiles fine, and gives the output "A created" followed by "B created". How is this happening? Or am I making some mistake in understanding the concept?

#include <iostream>
using namespace std;

class A{
public:
    A(void)
    {
        cout<<"A created"<<endl;
    }
    ~A()
    {
        //do nothing
    }
    void doSomething(void)
    {
        cout<<"hi";
    }
};    

class B:private A
{
    public:
    B(void)
    {
        cout<<"B created"<<endl;
    }
    ~B()
    {
        //do nothing
    }
};

int main() {
    B* myptr = new B();
    //myptr->doSomething();
    delete myptr;
    return 0;
}
mac93
  • 197
  • 1
  • 8
  • Are you wondering how private inheritance works at all? It has to, otherwise it wouldn't be in the language, now would it? – n. m. could be an AI Sep 08 '13 at 09:19
  • 2
    Private inheritance is only useful in extremely limited cases. You really want to inherit only when making use of polymorphism, otherwise you can just make it a member instead of a parent. See, http://en.wikipedia.org/wiki/Composition_over_inheritance – Cramer Sep 08 '13 at 09:22
  • I read that it has some uses when it comes to virtual functions. But yes, if the private constructor case is true, it does not make much sense! – mac93 Sep 08 '13 at 09:23
  • 1
    What is declared `private` here is the fact that `B` inherits from `A`. Other classes will not be aware of the inheritance. The constructor of `A` is still public and can be used. – Steve Sep 08 '13 at 09:29
  • 1
    What @steve said, but there are subtleties: for example, a private base class will participate in overload resolution (access checks happen after overload resolution). So the clients are exposed to the private base class. It is quite a tight coupling. There is an interesting discussion of non-public inheritance [in this GOTW](http://www.gotw.ca/publications/mill06.htm). – juanchopanza Sep 08 '13 at 09:33
  • possible duplicate of [Difference between private, public and protected inheritance in C++](http://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance-in-c) – Roman Nikitchenko Sep 08 '13 at 09:40

3 Answers3

3

B can call public (and protected) methods of A, since A constructor is public B can call it.

Please see following link to better understand c++ private inheritance: Difference between private, public, and protected inheritance

Community
  • 1
  • 1
dngot_
  • 413
  • 4
  • 9
  • Typo: "constrator" -> "constructor" (and you should capitalize "Please" and "C++"), but that aside, +1 :) Another related link: [Why do we actually need Private or Protected inheritance in C++?](http://stackoverflow.com/questions/374399/why-do-we-actually-need-private-or-protected-inheritance-in-c) – gx_ Sep 08 '13 at 09:29
0

The access specifier for inheritance limits access to code outside the derived class; think to the base class A as if it were a normal private field:" the outside" can't see it, but B can use it freely from "the inside".

As for the constructor, it's implicitly called by B's constructor, so there's no problem (and, besides, otherwise private inheritance would be unusable).

Still, don't worry too much about private inheritance, it's useful in extremely rare cases (I've never seem it actually used in real life, and for this reason many languages don't even support it), normally composition (using a normal private field) is a better and simpler way.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
0

Here is good short article about private inheritance which illustrates in details what is private inheritance, when it is useful and when it should be avoided in favour of inclusion.

In short:

  • Inheritance access modifier limits access to basic class properties and methods not for derived class but for code which uses derived class.
  • This is useful to 'replace' (not 'extend') basic class interface for users of inherited class.
  • In general it's considered better to include basic class member rather than use private inheritance.
  • If you need to reuse some methods of basic class which rely on virtual functions and you need to override virtual functions (but you still want to hide part of basic class interface from external code), this is more appropriate case for private inheritance.

Hope this will help.

Roman Nikitchenko
  • 12,800
  • 7
  • 74
  • 110