Note that there are some explanatory texts on larger screens.

plurals
  1. POerror: 'void Base::output()' is protected within this context
    primarykey
    data
    text
    <p>I'm confused about the errors generated by the <a href="http://codepad.org/dGA5KLYs" rel="nofollow noreferrer">following code</a>. In Derived::doStuff, I can access Base::output directly by calling it.</p> <p>Why can't I create a pointer to <code>output()</code> in the same context that I can call <code>output()</code>?</p> <p>(I thought protected / private governed whether you could use a name in a specific context, but apparently that is incomplete?) </p> <p>Is my fix of writing <code>callback(this, &amp;Derived::output);</code> instead of <code>callback(this, Base::output)</code> the correct solution?</p> <pre><code>#include &lt;iostream&gt; using std::cout; using std::endl; template &lt;typename T, typename U&gt; void callback(T obj, U func) { ((obj)-&gt;*(func))(); } class Base { protected: void output() { cout &lt;&lt; "Base::output" &lt;&lt; endl; } }; class Derived : public Base { public: void doStuff() { // call it directly: output(); Base::output(); // create a pointer to it: // void (Base::*basePointer)() = &amp;Base::output; // error: 'void Base::output()' is protected within this context void (Derived::*derivedPointer)() = &amp;Derived::output; // call a function passing the pointer: // callback(this, &amp;Base::output); // error: 'void Base::output()' is protected within this context callback(this, &amp;Derived::output); } }; int main() { Derived d; d.doStuff(); } </code></pre> <p>Edit: I'd love to know where this is in the stardard, but mostly I'm just trying to wrap my head around the concept. I think my problem is that <code>callback</code> doesn't have access to protected members of <code>Derived</code>, but it is able to call <code>Derived::output</code> if you pass it a pointer. How is a protected member of <code>Derived</code> that comes from <code>Derived</code> different from a protected member of <code>Derived</code> that comes from <code>Base</code>?</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.
 

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