在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