Re: Private and Protected Inheritance
On May 5, 12:22 am, Marcelo De Brito <Nosopho...@gmail.com> wrote:
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
}
};