简体   繁体   English

如果我覆盖它,我可以调用基类的虚拟 function 吗?

[英]Can I call a base class's virtual function if I'm overriding it?

Say I have classes Foo and Bar set up like this:假设我有这样的类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;
    }
};

As annotated in the code, I'd like to be able to call the base class's function that I'm overriding.如代码中所述,我希望能够调用我正在覆盖的基类的 function。 In Java there's the super.funcname() syntax.在 Java 中有super.funcname()语法。 Is this possible in C++?这在 C++ 中是否可行?

The C++ syntax is like this: C++ 语法如下:

class Bar : public Foo {
  // ...

  void printStuff() {
    Foo::printStuff(); // calls base class' function
  }
};

Yes,是的,

class Bar : public Foo
{
    ...

    void printStuff()
    {
        Foo::printStuff();
    }
};

It is the same as super in Java, except it allows calling implementations from different bases when you have multiple inheritance.它与 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();
    }
};

Sometimes you need to call the base class' implementation, when you aren't in the derived function...It still works:有时你需要调用基类的实现,当你不在派生的 function 中时......它仍然有效:

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;
}

Just in case you do this for a lot of functions in your class:以防万一您为 class 中的许多功能执行此操作:

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

class Bar : public Foo {
private:
  typedef Foo super;
public:
  void f1() {
    super::f1();
  }
};

This might save a bit of writing if you want to rename Foo.如果你想重命名 Foo. 这可能会节省一些写作。

If you want to call a function of base class from its derived class you can simply call inside the overridden function with mentioning base class name(like Foo::printStuff() ). If you want to call a function of base class from its derived class you can simply call inside the overridden function with mentioning base class name(like Foo::printStuff() ).

code goes here代码在这里

#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();
}

Again you can determine at runtime which function to call using the object of that class(derived or base).But this requires your function at base class must be marked as virtual. Again you can determine at runtime which function to call using the object of that class(derived or base).But this requires your function at base class must be marked as virtual.

code below下面的代码

#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();
}

If there are multiple levels of inheritance, you can specify the direct base class, even if the actual implementation is at a lower level.如果 inheritance 有多个级别,可以指定直接基 class,即使实际实现在较低级别。

class Foo
{
public:
  virtual void DoStuff ()
  {
  }
};

class Bar : public Foo
{
};

class Baz : public Bar
{
public:
  void DoStuff ()
  {
    Bar::DoStuff() ;
  }
};

In this example, the class Baz specifies Bar::DoStuff() although the class Bar does not contain an implementation of DoStuff .在此示例中,class Baz指定Bar::DoStuff()尽管 class Bar不包含DoStuff的实现。 That is a detail, which Baz does not need to know.这是一个细节,巴兹不需要知道。

It is clearly a better practice to call Bar::DoStuff than Foo::DoStuff , in case a later version of Bar also overrides this method.显然,调用Bar::DoStuff比调用Foo::DoStuff更好,以防更高版本的Bar也覆盖此方法。

check this...检查这个...

#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;
}

output output

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

the best way is using the base::function as say @sth最好的方法是使用base::function如@sth

Yes you can call it.是的,您可以调用它。 C++ syntax for calling parent class function in child class is C++ 在子 ZA2F2ED4F8EBC2CBB4C21A29DC40AB61 中调用父 class function 的语法是D

class child: public parent {
  // ...

  void methodName() {
    parent::methodName(); // calls Parent class' function
  }
};

Read more about function overriding .阅读有关 function覆盖的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能在派生类重写函数中调用受保护的虚拟基类函数? - Why can't I call protected virtual base class function in the derived class overridden function? 如何使用std :: bind()来调用基类的虚函数版本? - How do I use std::bind() to call the base class's version of a virtual function? 如何调用基类的虚拟函数定义,该基类在C ++中的抽象基类和派生类中均具有定义? - How can I call virtual function definition of base class that have definitions in both abstract base class and derived class in C++? 为什么我不能从基类槽Qt调用虚函数 - Why can not I call virtual function from base class slot Qt 如何在不进行类型转换和使用多态的情况下使用基类指针来调用派生类非虚拟成员函数? - How can I call derived class non virtual member function using base class pointer without typecasting and using polymorphism? 如何从 function 调用派生类的方法,期望基本 class 指针? - How can I call a derived class's method from a function expecting the a base class pointer? 如何调用基类的虚拟 function,它是 function 的输入参数 - How to call a base class's virtual function that is an input argument to a function 为什么“基类对象”不能称之为自己的虚函数? C ++ - Why can't a 'Base Class object' call it's own virtual function? C++ 如何调用基类的operator ==? - How can I call base class's operator==? 我可以在派生类中记录虚拟成员而不重写它吗? - Can I document a virtual member in a derived class without overriding it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM