Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can only access <code>protected</code> members from your own base class instance... not one provided to you as a parameter. It's all about OO encapsulation really. Without this restriction, the object under construction could invalidate invariants of the <code>baseTest&amp;</code> parameter.</p> <p>Put another way, your <code>SubTest</code> may decide on a use for a <code>protected</code> member that conflicts with the usage made of the same member by another <code>BaseTest</code>-derived class (say <code>SubTest2 : BaseTest</code>). If your <code>SubTest</code> code was allowed to fiddle with the other object's data, it could invalidate the invariants in a <code>SubTest2</code> object, or get some values out that were - in the intended encapsulation - only meant to be exposed to <code>SubTest2</code> and (optionally - see below) <code>SubTest2</code> derivatives.</p> <blockquote> <p>Followup question: Why is it, that in the added copy constructor I can access protected members of another instance?</p> </blockquote> <pre><code>SubTest(const SubTest&amp; x); // can access x._protMember SubTest(const BaseTest&amp; x); // cannot access x._protMember </code></pre> <p>The same insights above explain why this is allowed: the copy constructor gets a <code>SubTest&amp;</code> rather than just any old object derived from <code>BaseTest</code>, and this constructor is clearly within the <code>SubTest</code> abstraction. The <code>SubTest</code> coder is assumed to be conversant with the intended design/encapsulation <code>SubTest</code> provides, and the copy constructor is given access to bypass and enforce post-conditions/invariants on the other <code>SubTest&amp;</code> object too. (You are copying from an object that might itself have been copy-constructed by the very same function, so protecting it when on the "<code>*this</code>" side but not the parameter-by-ref side isn't much protection at all, even ignoring all the sound reasons you may want/need that access).</p> <p>It is possible that a <code>SubTest</code>-derived object will be accidentally passed to the <code>SubTest</code> copy constructor ("slicing"), but even for that scenario the <code>SubTest&amp;</code> class can control whether the further-derived object could have been doing anything unexpected with <code>_protMember</code> - adding a <code>private</code> <code>using BaseTest::_protMember;</code> statement if it wants to "finalise" access to <code>_protMember</code> and forbid any derived classes from using it.</p>
    singulars
    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. 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