far*_*oft 1 c++ overriding overloading
它是重载或覆盖还是其他功能?(你好功能)
class A {
public:
void hello(int x) { cout << "A" << endl; }
};
class B : public A {
public:
void hello() { cout << "B" << endl; }
};
void main() {
B obj;
obj.hello();
}
Run Code Online (Sandbox Code Playgroud)
它不是,它的功能隐藏.
在派生类中声明具有相同名称的非虚函数(即使签名不同)会完全隐藏基类实现.
要仍然可以访问A::hello,您可以执行以下操作:
class B : public A {
public:
using A::hello;
void hello() { cout << "B" << endl; }
};
Run Code Online (Sandbox Code Playgroud)
struct A
{
virtual void foo() {}
};
struct B : public A
{
/*virtual*/ void foo() {}
};
Run Code Online (Sandbox Code Playgroud)
struct A
{
void foo() {}
void foo(int) {}
};
Run Code Online (Sandbox Code Playgroud)