如果我重写它,我可以调用基类的虚函数吗?
假设我有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 + +?
C ++语法如下所示:
class Bar : public Foo { // ... void printStuff() { Foo::printStuff(); // calls base class' function } };
是,
class Bar : public Foo { ... void printStuff() { Foo::printStuff(); } };
它与Java中的super
相同,只是它允许在有多个inheritance时从不同基地调用实现。
class Foo { public: virtual void foo() { ... } }; class Baz { public: virtual void foo() { ... } }; class Bar : public Foo, public Baz { public: virtual void foo() { // Choose one, or even call both if you need to. Foo::foo(); Baz::foo(); } };
有时你需要调用基类的实现,当你不在派生函数中…它仍然有效:
struct Base { virtual int Foo() { return -1; } }; struct Derived : public Base { virtual int Foo() { return -2; } }; int main(int argc, char* argv[]) { Base *x = new Derived; ASSERT(-2 == x->Foo()); //syntax is trippy but it works ASSERT(-1 == x->Base::Foo()); return 0; }
以防万一你在你的课上做了很多function:
class Foo { public: virtual void f1() { // ... } virtual void f2() { // ... } //... }; class Bar : public Foo { private: typedef Foo super; public: void f1() { super::f1(); } };
如果你想重命名Foo,这可能会节省一些写作。
如果你想从它的派生类调用一个基类的函数,你可以简单地在提到的基类名称(如Foo :: printStuff() )中调用覆盖函数。
代码在这里
#include <iostream> using namespace std; class Foo { public: int x; virtual void printStuff() { cout<<"Base Foo printStuff called"<<endl; } }; class Bar : public Foo { public: int y; void printStuff() { cout<<"derived Bar printStuff called"<<endl; Foo::printStuff();/////also called the base class method } }; int main() { Bar *b=new Bar; b->printStuff(); }
同样,你可以在运行时确定使用该类的对象(派生或基本)调用哪个函数。但是这需要你在基类中的函数必须标记为虚拟。
下面的代码
#include <iostream> using namespace std; class Foo { public: int x; virtual void printStuff() { cout<<"Base Foo printStuff called"<<endl; } }; class Bar : public Foo { public: int y; void printStuff() { cout<<"derived Bar printStuff called"<<endl; } }; int main() { Foo *foo=new Foo; foo->printStuff();/////this call the base function foo=new Bar; foo->printStuff(); }
检查这个…
#include <stdio.h> class Base { public: virtual void gogo(int a) { printf(" Base :: gogo (int) \n"); }; virtual void gogo1(int a) { printf(" Base :: gogo1 (int) \n"); }; void gogo2(int a) { printf(" Base :: gogo2 (int) \n"); }; void gogo3(int a) { printf(" Base :: gogo3 (int) \n"); }; }; class Derived : protected Base { public: virtual void gogo(int a) { printf(" Derived :: gogo (int) \n"); }; void gogo1(int a) { printf(" Derived :: gogo1 (int) \n"); }; virtual void gogo2(int a) { printf(" Derived :: gogo2 (int) \n"); }; void gogo3(int a) { printf(" Derived :: gogo3 (int) \n"); }; }; int main() { std::cout << "Derived" << std::endl; auto obj = new Derived ; obj->gogo(7); obj->gogo1(7); obj->gogo2(7); obj->gogo3(7); std::cout << "Base" << std::endl; auto base = (Base*)obj; base->gogo(7); base->gogo1(7); base->gogo2(7); base->gogo3(7); std::string s; std::cout << "press any key to exit" << std::endl; std::cin >> s; return 0; }
产量
Derived Derived :: gogo (int) Derived :: gogo1 (int) Derived :: gogo2 (int) Derived :: gogo3 (int) Base Derived :: gogo (int) Derived :: gogo1 (int) Base :: gogo2 (int) Base :: gogo3 (int) press any key to exit
最好的方法是使用base :: function,像@sth