运行超类重写的函数
run superclasses overridden function
如何从子类中重写的函数中调用超类中的重写函数?
例如:class super有一个叫做foo的函数,它在一个叫做sub的子类中被覆盖,如何让subs foo调用super foo?
您可以利用继承!
class A
{
public:
virtual void Foo()
{
// do some base class foo-ing
}
};
class B : public A
{
public:
virtual void Foo()
{
// do some subclass foo-ing
// to call the super-class foo:
A::Foo( );
}
};
void main()
{
B obj;
obj.Foo( );
// This is if you want to call the base class Foo directly using the instance obj
A* handle = &obj;
handle->Foo( );
}
我想您谈论的是覆盖,而不是重载。对函数的合格调用不会使用动态调度机制,您可以控制选择什么覆盖:
struct base {
virtual void foo() {...}
};
struct derived : base {
virtual void foo() {
base::foo(); // call the override at 'base' level
...
}
};
如果你真的在谈论重载,你可以使用相同的机制,或者你可以将重载带入派生类型的范围:
struct base {
void foo(int);
};
struct derived : base {
using base::foo; // make all overloads of base::foo available here
void foo(double);
void f() { base::foo(1); } // alternatively qualify the function call
};
您可以使用super::foo。例如:
#include <stdio.h>
class A
{
public:
void foo(void)
{
printf("Class An");
}
};
class B : public A
{
public:
void foo(void)
{
printf("Class Bn");
A::foo();
}
};
int main ()
{
B b;
b.foo();
}
相关文章:
- 在 C++ 中用派生类型重写成员函数
- 重写虚拟函数和继承
- C++调用使用重写函数的父类函数
- 重写打印函数而不是覆盖基类
- 重写函数不打印基类数据
- 将一个小的 C 定义重写为"normal" C++函数
- 重写另一个方法 [C++] 使用的超类回调函数
- 具有派生参数的函数重写
- 在回调中使用函数时,C++未知重写说明符
- 用模板化函数重写虚拟函数
- 没有函数重写的多重继承.为什么模棱两可
- C++ 继承构造函数重写
- 关于C++中的虚拟函数重写
- C++函数重写不起作用
- 如何实现类变量的虚拟函数重写
- 将C++函数重写为C#(将指针传递到数组中的下一个元素)
- 引用可以用于实现函数重写吗
- 用函数重写嵌套的if-else语句
- C++继承函数重写
- 本机C++中的函数重写