基本上,我的问题与名称查找和使用
声明(http://en.cppreference.com/w/cpp/language/namespace)有关。
假设我们有以下(绝对是愚蠢的)代码:
class Base {
public:
void fun()
{std::cout << "Base fun" << std::endl;}
};
class Derived : public Base {
public:
// Here both names "fun" are visible or not?
using Base::fun;//let's call this func_1
void fun() //let's call this func_2
{
std::cout << "Derived fun" << std::endl;
}
};
Derived d;
d.fun(); // This resolves to func_2, why?
因此,我的理解是现在我们应该让两个 名称都可见,然后对于名称查找,应该有一些歧义。但实际上并非如此。是什么原因,或者换句话说,我是否误解了一些概念?
最佳答案
标准对这种情况有特殊规定。
When a using-declaration brings names from a base class into a derived class scope, member functions and member function templates in the derived class override and/or hide member functions and member function templates with the same name, parameter-type-list (8.3.5), cv-qualification, and ref-qualifier (if any) in a base class (rather than conflicting).
([命名空间.udecl]/15)
请注意,与往常一样,您可以通过执行 d.Base::fun()
强制调用 Base::fun
。
关于c++ - 使用声明对两个完全相同名称的名称查找的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36635372/