可以从具有相同名称的派生 class 函数调用基 class 函数。但我找不到支持这一点的标准引述

One can call a base class function from a derived class function with the same name. But I can't find a quote from the Standard supporting this

考虑以下片段:

struct Base{
   void f() {}
};

struct Derived: public Base{
   void f() { Base::f(); }
};

Derived d;
int main(){
    d.f();
}

代码段工作正常。即,d.f() 调用 Base::f()。但是我找不到支持这个的标准的引用。

以下所有标准参考均参考 N4659: March 2017 post-Kona working draft/C++17 DIS


这是[class.qual]/1[摘录,强调我的]:

If the nested-name-specifier of a qualified-id nominates a class, the name specified after the nested-name-specifier is looked up in the scope of the class [...]

在您的示例中,Base:: 嵌套名称说明符 ,其后指定的名称是 f(),因此查找在classBase的范围内执行。此规则不会受到调用站点上的阴影的影响,因为我们使用的是限定名称,并且我们可能会注意到,如果我们从 Base 中删除 f(),则限定 ID 将(正如限定查找所预期的那样) ) 不搜索 f() 的派生范围 class.

struct Base {};

struct Derived: public Base {
   void f() { 
       Base::f();  // error: 'f' is not a member of 'Base
   }
};

我们可能还注意到[class.qual]/3特别提到了这个案例:

A class member name hidden by a name in a nested declarative region or by the name of a derived class member can still be found if qualified by the name of its class followed by the ​::​ operator.

但正如指出的那样,这一段可以说是非规范的(1).


(1) [class.qual]/3 给出了一个具体的例子说明[class.qual]/1 是如何应用/发挥它的作用的。然而,标准中的这些部分通常是非规范的(示例、注释等),而 [class.qual]/3 没有带来“新的规范”价值(已在 [class.qual]/1), 没有包装成注释或例子。