C++ 有 super() 方法没?
显然没有。为什么?
因为C++ 支持 多重继承,所以super()无法获得究竟是哪一个父类的同名方法。
那么该如何做呢?
只能用 父类名::方法名 这样来调用了。
Example:
#include <iostream>
using namespace std;
class Base
{
public:
void f()
{
cout << "Base::f" << endl;
}
};
class Derived : public Base
{
public:
void f()
{
cout << "Derived::f" << endl;
Base::f();
}
};
int main()
{
Derived d;
d.f();
d.Base::f();
return 0;
}
/*
Derived::f
Base::f
Base::f
Press any key to continue
*/