Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>using</code> declaration doesn't define a function. It "declares a name" (not a function!), and unhides base names. It's true that the declaration itself has its own accessibility level, which is why you're using it at all in the first place, but to stress again: The using declaration does not declare a new member function. E.g. C++11 7.3.3/11:</p> <blockquote> <p>The entity declared by a <em>using-declaration</em> shall be known in the context using it according to its definition at the point of the using-declaration.</p> </blockquote> <p>"Its definition", in your case, is still <code>void base::test(){}</code>, and <em>that</em> is the entity that is made known to the derived class and referred to by <code>&amp;derived::test</code>. You can't get a function pointer of type <code>void(derived:**)()</code> from this because there is no such function.</p> <p>When you say <code>&amp;derived::test</code>, the <code>&amp;</code>-operator is used, which works like this (5.3.1/3):</p> <blockquote> <p>The result of the unary <code>&amp;</code> operator is a pointer to its operand. The operand shall be an lvalue or a <em>qualified-id</em>. If the operand is a <em>qualified-id</em> naming a non-static member <code>m</code> of some class <code>C</code> with type <code>T</code>, the result has type “pointer to member of class <code>C</code> of type <code>T</code>” and is a prvalue designating C::m.</p> </blockquote> <p>In my interpretation of the above logic, <code>&amp;derived::test</code> "names the non-static member <code>test</code> of class <code>base</code>". [Thanks to @DyP:] The is formalized in 10.2/3:</p> <blockquote> <p>The lookup set for <code>f</code> in <code>C</code> [...] consists of [...] the declaration set [...]. In the declaration set, <em>using-declarations</em> are replaced by the members they designate</p> </blockquote> <p>If you really want, you can provide a wrapper like this:</p> <pre><code>class derived : protected base { public: void test() { base::test(); } }; </code></pre> <p>(Curiously, that solution actually <em>does</em> hide the base function, which is what we want. Rather than using <code>using</code>, we use an explicitly qualified name to refer to the base function.)</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