名称隐藏和访问基类的非虚函数(语法)
Name hiding and access base class' non-virtual function (syntax)
在以下代码中:
#include <iostream>
class A
{
public:
void f( float x ) { std::cout << 1; }
void g() { std::cout << 11; }
};
class B : public A
{
public:
void f( char x ) { std::cout << 2; }
void g() { std::cout << 22; }
};
int main()
{
B b;
b.A::f( 0 );
b.A::g();
return 0;
}
这个名字是不是隐藏起来了?在标准中定义的语法在哪里(c++ 11或c++ 03,没关系,这两个标准似乎是相同的)?
我根本不知道这是可能的,这是我第一次看到这样的语法(在这里第一次看到它:为什么我不能访问下面代码中的A类函数?)
是名称隐藏。因此,它不是重载(也不是重写)。N3485
中的13.2 Declaration matching
节对此进行了解释。
13.2 Declaration matching
1 Two function declarations of the same name refer to the same function if they are in
the same scope and have equivalent parameter declarations (13.1). A function member of
a derived class is not in the same scope as a function member of the same name in a base class.
[ Example:
struct B {
int f(int);
};
struct D : B {
int f(const char*);
};
Here D::f(const char*) hides B::f(int) rather than overloading it.
void h(D* pd) {
pd->f(1); // error:
// D::f(const char*) hides B::f(int)
pd->B::f(1); // OK
pd->f("Ben"); // OK, calls D::f
}
——结束例子)
相关文章:
- C++使用 rand 定义函数语法
- QObject::连接不起作用 - 使用函数语法找不到信号
- C++ std::函数语法问题
- 如何在不更改现有函数语法的情况下将普通指针替换为共享指针
- 模板化函数语法错误中使用的模板化类中的嵌套类
- C 变化函数语法
- 返回语句中的构造函数语法
- 复制构造函数语法错误:myclass 没有成员 mymember
- C++.传递给函数.语法问题
- C++可视化工作室函数语法突出显示
- 在 c++ 中移动 2d 数组的构造函数(语法逻辑不清楚):
- c 通过值或指针函数语法
- 正确的朋友模板函数语法
- C++类构造函数语法的解释
- C++ 使用指针移动构造函数(*&&&语法)
- 复制构造函数语法并显示构造函数的值
- 回调函数语法
- 类模板专用化中的成员函数语法
- C++学习函数语法
- 使用模板 C++ 的友元函数语法错误