What is the difference between secure and private derivation in C ++

Possible duplicate:
The difference between private, open and protected inheritance in C ++

What is the difference between getting as protected or closed in C ++? I cannot understand, since both seem to restrict access to a member of the base class from a derived class object

+8
c ++
source share
5 answers

Consider a code example showing what will be allowed (or not) using different levels of inheritance:

class BaseClass {}; void freeStandingFunction(BaseClass* b); class DerivedProtected : protected BaseClass { DerivedProtected() { freeStandingFunction(this); // Allowed } }; 

DerivedProtected can go to freeStandingFunction because it knows that it comes from BaseClass .

  void freeStandingFunctionUsingDerivedProtected() { DerivedProtected nonFriendOfProtected; freeStandingFunction(&nonFriendOfProtected); // NOT Allowed! } 

An alien (class, function, whatever) cannot pass DerivedProtected to freeStandingFunction because inheritance is protected and therefore not visible outside derived classes. The same goes for private inheritance.

  class DerivedFromDerivedProtected : public DerivedProtected { DerivedFromDerivedProtected() { freeStandingFunction(this); // Allowed } }; 

A class derived from DerivedProtected can say that it inherits from BaseClass , so it can go to freeStandingFunction .

  class DerivedPrivate : private BaseClass { DerivedPrivate() { freeStandingFunction(this); // Allowed } }; 

The DerivedPrivate class DerivedPrivate knows that it comes from BaseClass , so it can go to freeStandingFunction .

 class DerivedFromDerivedPrivate : public DerivedPrivate { DerivedFromDerivedPrivate() { freeStandingFunction(this); // NOT allowed! } }; 

Finally, the non-friend class, located further down the inheritance hierarchy, cannot see DerivedPrivate inheriting from BaseClass , so it cannot go to freeStandingFunction .

+9
source share

Use this matrix (taken from here ) to determine the visibility of inherited members:

 inheritance \ member |  private |  protected |  public
 -------------------- + ----------------- + ----------- ---- + --------------
 private |  inaccessible |  private |  private
 protected |  inaccessible |  protected |  protected
 public |  inaccessible |  protected |  public
 -------------------- + ----------------- + ----------- ---- + --------------

Example 1:

 class A { protected: int a; } class B : private A {}; // 'a' is private inside B 

Example 2:

 class A { public: int a; } class B : protected A {}; // 'a' is protected inside B 

Example 3:

 class A { private: int a; } class B : public A {}; // 'a' is inaccessible outside of A 
+3
source share

private allows only the class for which it is declared; for access, protected allows access to the class and its derivatives / subclasses, as if it were closed

+1
source share

I have added a very detailed explanation about Inheritance and Access Specifiers in this Q. He explains all types of Inheritance and how access specifiers for each of them work. Take a look. Hth :)

+1
source share

Basically, protected inheritance continues further along the inheritance hierarchy than private inheritance. See the C ++ FAQ Lite for more details.

0
source share

All Articles