Private and Protected Inheritance

  • Thread starter Marcelo De Brito
  • Start date
M

Marcelo De Brito

Hi!

I do not know if it is a standard, but I got surprised when I made a
private inheritance and called, inside the derived class, a public/
protected member function from the base class without any compiler
complain (I got a similar feedback when using protected inheritance).
Is that a standard?

I thought that when it comes to private inheritance, the derived class
could not access any member from the base class, since all of them
would become private too.

If what I am saying is not clear enough, see the code below:

#include<iostream>
using namespace std;

class c1 {
public:
c1() : i(10) {cout << "c1::c1()" << endl;}
~c1() {cout << "c1::~c1()" << endl;}
void f() {cout << "c1::void f()" << endl;}
void g() {cout << "c1::int g()" << endl;}
protected:
int h() {
cout << "c1::int h()" << endl;
cout << "i = " << i << endl;
return(0);
}
private:
int i;
};

class c2 : private c1 {
public:
c2() : i(5) {cout << "c2::c2()" << endl;}
~c2() {cout << "c2::~c2()" << endl;}
void f1() {
cout << "c2::void f1()" << endl;
f(); // HERE
g(); // HERE
h(); // HERE
}
private:
int i;
};

int main()
{
c2 obj2;

obj2.f();

return(0);
}


I appreciate any comment, suggestion, etc.

Thank you!

Marcelo de Brito
 
V

Victor Bazarov

Marcelo said:
I do not know if it is a standard, but I got surprised when I made a
private inheritance and called, inside the derived class, a public/
protected member function from the base class without any compiler
complain (I got a similar feedback when using protected inheritance).
Is that a standard?

It's not "a" standard, but it is a language feature. The derived class
can access anything public or protected in the base class subobject.
The relationship between the derived class and the base class is not the
same as 'public' inheritance to the "outside observer".
I thought that when it comes to private inheritance, the derived class
could not access any member from the base class, since all of them
would become private too.

No. Anything public and protected in the base class is accessible to
the derived class *regardless* of the type of inheritance. It matters
only to the outside code.

V
 
N

Neelesh

Hi!

I do not know if it is a standard, but I got surprised when I made a
private inheritance and called, inside the derived class, a public/
protected member function from the base class without any compiler
complain (I got a similar feedback when using protected inheritance).
Is that a standard?

I thought that when it comes to private inheritance, the derived class
could not access any member from the base class, since all of them
would become private too.


No. There is a difference between a "private member" and a "privately
inherited member". A "private member" of the base class will not be
accessible by the member functions of the derived class. However, a
"privately inherited member" is accessible as a private member by the
member functions of the derived class.

Take this example:

class D1
{
void foo() { } //private, hence not accessible by member
functions of derived class

protected:
void bar() { } //protected, hence accessible by member functions
of the derived class, irrespective of the type of inheritance

public:
void baz() { } //public, hence accessible by member functions of
the derived class, irrespective of the type of inheritance
};

class D2: private D1 //private inheritance, hence protected and public
members of base class are accessible as private members of derived
class.
void test()
{
foo(); //ERROR
bar(); //OK
baz(); //OK
}
};
 
M

Marcelo De Brito

Hi!

Thank you very much guys for the explanations!

Best wishes!

Marcelo de Brito
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top