Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I do believe that you might want to reconsider the design, but a solution to the specific question of <em>how can I access the member?</em> could be:</p> <pre><code>class A{ protected: int var; static int&amp; varAccessor( A&amp; a ) { return a.var; } }; </code></pre> <p>And then in the derived type call the protected accessor passing the member object by reference:</p> <pre><code>varAccessor( this-&gt;myA ) = 5; </code></pre> <p>Now, if you are thinking on the decorator pattern, I don't think this is the way to go. The source of the confusion is that most people don't realize that a type has two separate interfaces, the <code>public</code> interface towards users and the <code>virtual</code> interface for implementation providers (i.e. derived types) as in many cases functions are both <code>public</code> and <code>virtual</code> (i.e. the language allows binding of the two <em>semantically</em> different interfaces). In the Decorator pattern you use the base interface to provide an implementation. Inheritance is there so that the derived type can <em>provide</em> the operation for the <em>user</em> by means of some actual work (decoration) and then forwarding the work to the actual object. The inheritance relationship is not there for you to access the implementation object in any way through protected elements, and that in itself is dangerous. If you are passed an object of a derived type that has stricter invariants regarding that protected member (i.e. for objects of type X, <code>var</code> must be an odd number), the approach you are taking would let a <em>decorator</em> (of sorts) break the invariants of that <code>X</code> type that should just be decorated.</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.
 

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