Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are hitting a small quirk in the syntax (not that C++ has few of them...). The default way to access a member variable is through direct use of the name or through <code>this-&gt;</code>. That is, the simpler spelling of your show function would be:</p> <pre><code>void B::show() { std::cout &lt;&lt; a &lt;&lt; std::endl; // alternatively this-&gt;a std::cout &lt;&lt; &amp;a &lt;&lt; std::endl; // &amp;(this-&gt;a) } </code></pre> <p>Which has a simple and consistent syntax. Now the language allows you to drop in extra qualifiers to access members of a base when accessing a member:</p> <pre><code>std::cout &lt;&lt; A::a &lt;&lt; std::endl; // Extra qualification </code></pre> <p>This is really equivalent to <code>this-&gt;A::a</code> and the main uses of the extra qualification are to disambiguate (if two bases had a member <code>a</code>, select the one in <code>A</code>) and in the case of virtual function disabling dynamic dispatch.</p> <p>Now you can do the same with pointers, as in <code>&amp;this-&gt;A::a</code>, which will take the address of the member <code>a</code> in the subobject <code>A</code> of the current object. The problem in this case is that you cannot drop the <code>this-&gt;</code> qualifier, as the syntax <code>&amp;A::a</code> is reserved to obtain a pointer to member, although by adding an extra set of parenthesis, as in <code>&amp;(A::a)</code> the parser can no longer interpret that as obtaining a pointer to member but rather as taking the address of the object represented by <code>A::a</code>, which as seen before is the member in the base.</p>
 

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