如果我要覆盖它,我可以调用基类的虚函数吗?

Ale*_*lex 315 c++ overriding virtual-functions

假设我有课程FooBar设置如下:

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;
    }
};
Run Code Online (Sandbox Code Playgroud)

正如在代码中注释的那样,我希望能够调用我所覆盖的基类函数.在Java中有super.funcname()语法.这在C++中是否可行?

sth*_*sth 417

C++语法是这样的:

class Bar : public Foo {
  // ...

  void printStuff() {
    Foo::printStuff(); // calls base class' function
  }
};
Run Code Online (Sandbox Code Playgroud)

  • @David:不,这样做是完全正常的,尽管它可能取决于你的实际课程,如果它真的有用.只有在你想要发生的事情时才调用基类方法;). (35认同)
  • 这对大多数人来说可能是显而易见的,但为了完整性,请记住永远不要在构造函数和析构函数中执行此操作. (23认同)
  • 当您的客户需要这样做时,请注意这是__code的气味!(称为"调用超级要求",请在此处查看:http://en.wikipedia.org/wiki/Call_super) (19认同)
  • 它也适用于非虚基类方法. (11认同)
  • 这样做有什么问题吗?这是不好的做法吗? (10认同)
  • @ v.oddou:当超级类有用时,调用它是没有错的.您链接的那个页面只是涉及继承的一些特定潜在问题.它甚至会说自己调用超类一般都没有错:*"请注意,调用父模式是反模式的**要求.实际代码中有很多例子,其中的方法是在子类中可能仍然需要超类的功能,通常只是在增加父功能."* (7认同)
  • 为了澄清上面所说的内容,我会说当调用基类方法是公共接口的一部分时,这是一种代码味道.但是,在内部执行此操作不应该是一个问题(即在一个模块中一起定义的一组类中). (3认同)
  • @sth:是的,这就是为什么我在所有大写中都需要写的.维基链接的灵感来源于Martin Fowler对此文章的反思:http://www.martinfowler.com/bliki/CallSuper.html (2认同)
  • 关于@Javy,要使用参数执行baseclass构造函数,可以使用以下语法`constructor(int param):baseclass(param){your code ...}`.重要的是要记住,对象是自下而上构造的,这意味着首先调用基类构造函数.另见[这里](http://stackoverflow.com/a/120916/258418) (2认同)
  • @TigerCoding 为什么不在构造函数中?当派生类调用任何东西时,基类的构造已经完成,所以调用基类方法是安全的。(析构函数的顺序相反,所以对他们来说,建议是有效的) (2认同)

Ale*_*x B 114

是,

class Bar : public Foo
{
    ...

    void printStuff()
    {
        Foo::printStuff();
    }
};
Run Code Online (Sandbox Code Playgroud)

它与superJava中的相同,只是它允许在具有多个继承时从不同的基础调用实现.

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();
    }
};
Run Code Online (Sandbox Code Playgroud)

  • 这是一个比选择的答案更好的答案.谢谢. (7认同)

Alw*_*ing 66

有时你需要调用基类的实现,当你不在派生函数中时...它仍然有效:

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,您既需要将x都设置为Base *类型,又需要使用Base :: Foo语法来使其正常工作 (2认同)

Mar*_*ner 27

为了防止你为你班上的很多功能做到这一点:

class Foo {
public:
  virtual void f1() {
    // ...
  }
  virtual void f2() {
    // ...
  }
  //...
};

class Bar : public Foo {
private:
  typedef Foo super;
public:
  void f1() {
    super::f1();
  }
};
Run Code Online (Sandbox Code Playgroud)

如果要重命名Foo,这可能会节省一些写入.

  • 如果你有超过1个基类怎么办?无论如何,试图将C++看起来像其他语言一样是愚蠢而且通常是徒劳的.只需记住基地的名称并通过它来称呼它. (5认同)
  • 这样做的方式很有趣,但不适用于多重继承。 (2认同)

Tun*_*her 6

如果要从派生类中调用基类函数,只需在提取基类名称(如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();
}
Run Code Online (Sandbox Code Playgroud)

同样,您可以在运行时确定使用该类的对象(派生或基础)调用哪个函数.但这要求您在基类中的函数必须标记为虚拟.

代码如下

#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();
}
Run Code Online (Sandbox Code Playgroud)