隐藏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
热门推荐
10 城市祝福语简短10字
11 古诗生日祝福语简短独特
12 老人过大寿祝福语简短
13 雪后早晨简短的祝福语
14 同事男生节祝福语简短
15 六一祝福语简短感动女生
16 感谢父母的简短祝福语
17 教练生日祝福语 简短独特
18 冬至婆婆的祝福语简短