Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's consider a code example showing what would be allowed (or not) using different levels of inheritance:</p> <pre><code> class BaseClass {}; void freeStandingFunction(BaseClass* b); class DerivedProtected : protected BaseClass { DerivedProtected() { freeStandingFunction(this); // Allowed } }; </code></pre> <p><code>DerivedProtected</code> can pass itself to <code>freeStandingFunction</code> because it knows it derives from <code>BaseClass</code>.</p> <pre><code> void freeStandingFunctionUsingDerivedProtected() { DerivedProtected nonFriendOfProtected; freeStandingFunction(&amp;nonFriendOfProtected); // NOT Allowed! } </code></pre> <p>A non-friend (class, function, whatever) cannot pass a <code>DerivedProtected</code> to <code>freeStandingFunction</code>, because the inheritance is protected, so not visible outside derived classes. Same goes for private inheritance.</p> <pre><code> class DerivedFromDerivedProtected : public DerivedProtected { DerivedFromDerivedProtected() { freeStandingFunction(this); // Allowed } }; </code></pre> <p>A class derived from <code>DerivedProtected</code> can tell that it inherits from <code>BaseClass</code>, so can pass itself to <code>freeStandingFunction</code>.</p> <pre><code> class DerivedPrivate : private BaseClass { DerivedPrivate() { freeStandingFunction(this); // Allowed } }; </code></pre> <p>The <code>DerivedPrivate</code> class itself knows that it derives from <code>BaseClass</code>, so can pass itself to <code>freeStandingFunction</code>.</p> <pre><code>class DerivedFromDerivedPrivate : public DerivedPrivate { DerivedFromDerivedPrivate() { freeStandingFunction(this); // NOT allowed! } }; </code></pre> <p>Finally, a non-friend class further down the inheritance hierarchy cannot see that <code>DerivedPrivate</code> inherits from <code>BaseClass</code>, so cannot pass itself to <code>freeStandingFunction</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload