Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working mechanism of private inheritance of a class having private constructor

Tags:

c++

case 1:

class ObjectCount {
private:
    ObjectCount(){}
};

class Employee : private ObjectCount {};

case 2:

class ObjectCount {
public:
    ObjectCount(){}
};

class Employee : private ObjectCount {};

In case1: ObjectCount constructor is private and inheritance is private . It gives compiler error

In case2: ObjectCount constructor is public and inheritance is private . this code is ok.

Can anyone explain how is it happening?

like image 256
sukumar Avatar asked Aug 26 '11 09:08

sukumar


1 Answers

In first case, the Employee C'tor cannot invoke its parent (ObjectCount) C'tor, because it is private.

In the 2nd case, there is no problem for the Employee C'tor to invoke the parent's ctor, since it is public.

Note that this is important since every class must use its parent constructor before activating its own.

The private inheritence means that other classes cannot use [or see] Employee as a ObjectCount, it doesn't change the visibility of ObjectCount's c'tor, which must be accessable by the derived class in any case.

like image 69
amit Avatar answered Oct 07 '22 11:10

amit