隐藏C ++基类中的所有重载方法
在C++中,我们可以使用函数重载技术。但是,如果某个基类具有一种重载形式的方法(具有相同名称的不同功能签名),并且派生类重新定义了基体内存在的功能之一,则该功能的所有重载版本都将被隐藏派生类。
让我们看一个例子来弄清楚这个想法。
示例
#include <iostream>
using namespace std;
class MyBaseClass {
public:
void my_function() {
cout << "This is my_function. This is taking no arguments" << endl;
}
void my_function(int x) {
cout << "This is my_function. This is taking one argument x" << endl;
}
};
class MyDerivedClass : public MyBaseClass {
public:
void my_function() {
cout << "This is my_function. From derived class, This is taking no arguments" << endl;
}
};
main() {
MyDerivedClass ob;
ob.my_function(10);
}输出结果
[Error] no matching function for call to 'MyDerivedClass::my_function(int)' [Note] candidate is: [Note] void MyDerivedClass::my_function() [Note] candidate expects 0 arguments, 1 provided
热门推荐
7 团圆日祝福语简短
10 孩子妈妈的祝福语简短
11 内涵生日祝福语 简短独特
12 离世人的祝福语简短
13 恭祝发财祝福语大全简短
14 送给外婆祝福语简短的话
15 中考感言简短祝福语大全
16 送同事离职祝福语简短
17 最新中午问候祝福语简短
18 停学不停课祝福语简短