假设我有类 Foo
和 Bar
设置如下:
class Foo
{
public:
int x;
virtual void printStuff()
{
std::cout << x << std::endl;
}
};
class Bar : public Foo
{
public:
int y;
void printStuff()
{
// I would like to call Foo.printStuff() here...
std::cout << y << std::endl;
}
};
如代码中所述,我希望能够调用我正在覆盖的基类函数。在 Java 中有 super.funcname()
语法。这在 C++ 中可能吗?
原文由 Alex 发布,翻译遵循 CC BY-SA 4.0 许可协议
在 C++ 中,您必须在调用派生类方法时显式命名基类。这可以通过派生类的任何方法来完成。覆盖是同名方法的特例。在 Java 中没有多重继承,因此您可以使用 super 来唯一命名基类。 C++ 语法是这样的: