title: How can a derived class use a protected member of the base class?
body: <p>Say that a base class <code>A</code> defines a protected member. A derived class <code>B</code> uses this member.</p>
  
  <pre><code>class A
  {
  public:
    A(int v) : value(v) { }
  
  protected:
    int value;
  };
  
  class B : public A
  {
  public:
    B(int v) : A(v) { }
    void print() const;
    void compare_and_print(const A&amp; other) const;
  };
  </code></pre>
  
  <p>The function <code>B::print</code> just takes the value of the current member and prints it:</p>
  
  <pre><code>void B::print() const
  {
    std::cout &lt;&lt; "Value: " &lt;&lt; value &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>The other member function, <code>B::compare_and_print</code>, takes an instance of <code>A</code>, checks their values and prints the maximum of both:</p>
  
  <pre><code>void B::compare_and_print(const A&amp; other) const
  {
    auto max_value = std::max(value, other.value);
    std::cout &lt;&lt; "Max value: " &lt;&lt; max_value &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>If <code>other</code> were an instance of the class <code>B</code>, this would be no problem. But the function would like to work with any kind of <code>A</code> instances. This does unfortunately not compile. This is what clang says about this:</p>
  
  <pre><code>test.cpp:27:42: error: 'value' is a protected member of 'A'
    auto max_value = std::max(value, other.value);
                                           ^
  test.cpp:9:7: note: can only access this member on an object of type 'B'
    int value;
        ^
  1 error generated.
  </code></pre>
  
  <p>This restriction sounds counter-intuitive to me. However, I'm not going to dispute the C++ standard about this (I'm nevertheless interested about the rationale behind this decision).</p>
  
  <p>My problem is that in my project I really have this kind of use case: A derived class has a method that takes an instance of the base class and needs to access a protected member of the received object.</p>
  
  <p>The easiest solution, the one I implemented currently, is to add a public member function to the base class, which returns the protected member. This solution does not fully satisfy me because I would like to avoid exporting the member this way.</p>
  
  <p><strong>How can I enable the usage of the base class' protected member by the derived class without exporting the member through the API?</strong></p>
  
  <hr>
  
  <p>EDIT:
  Some related questions are:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/3247671/accessing-protected-members-in-a-derived-class">Accessing protected members in a derived class</a></li>
  <li><a href="https://stackoverflow.com/questions/9139824/how-to-access-protected-members-in-a-derived-class">How to access protected members in a derived class?</a></li>
  </ul>
  
  <p>The answer I would like to have is the explanation of a design pattern to solve this problem without exposing the the protected member to the external code (where <em>external</em> means, code that is not part of the framework defining these classes).</p>
  
  <p>It could be that such a pattern cannot exist, I acknowledge.</p>