简体   繁体   English

C ++使基类在子类中使用重写的方法

[英]c++ make the base class use overridden methods in the child class

I'm trying to override a method of the base class used by another method in the base class; 我试图覆盖基类中的另一个方法所使用的基类方法; however, when the derived class invokes the using-method of the base class, the derived used-method is never executed, but instead the base class's used-method is called. 但是,当派生类调用基类的use-method时,将永远不会执行派生的used-method,而是调用基类的used-method。 Here's an example: 这是一个例子:

#include <iostream>
using namespace std;
class Base {
public:
    Base() {}
    virtual ~Base() {}
    void printLeft() { cout << this->getLeft(); }
    int getLeft() { return 0; }
};

class Derived: public Base {
public:
    Derived() {}
    virtual ~Derived() {}
    int getLeft() { return 1; }
};
int main(int argc, char *argv[]) {
    Derived d = Derived();
    d.printLeft();
}

Running main() prints 0 , indicating Base 's getLeft() method was used rather than the derived object's method. 运行main()显示0 ,表明使用了BasegetLeft()方法,而不是派生对象的方法。

How can I change this code so Derived::getLeft() is called when called from an instance of Derived ? 我如何更改此代码,以便从Derived实例调用Derived::getLeft() 时被调用

You just need to make getLeft virtual: 您只需要使getLeft虚拟:

class Base {
public:
    Base() {}
    virtual ~Base() {}
    void printLeft() { cout << this->getLeft(); }
    virtual int getLeft() { return 0; }
};

By default in C++ , member functions are not virtual. 在C ++中,默认情况下,成员函数不是虚拟的。 That is, you can't override them in a subclass. 也就是说,您不能在子类中覆盖它们。

For non-virtual functions, the static type of the object is used to determine which class's method is called. 对于非虚函数,对象的静态类型用于确定调用哪个类的方法。

In your case, you are calling getLeft() from Base::printLeft() . 在你的情况,你在呼唤getLeft()Base::printLeft() The type of this is Base* , so the function called will be Base::getLeft() . 该类型的thisBase* ,所谓的功能将Base::getLeft()

The way around this is to use the virtual keyword. 解决此问题的方法是使用virtual关键字。 In that case, the virtual function table will be used to determine which version of getLeft() to call, in this case Derived. 在这种情况下,虚拟函数表将用于确定要调用的getLeft()版本,在这种情况下为Derived.

You can trigger this behavior by prepending virtual to the declarations of printLeft in both Base and Derived . 您可以通过在BaseDerivedprintLeft声明前添加virtual来触发此行为。

暂无
暂无

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

相关问题 在C ++中处理基类中重写的方法的异常 - Handling exceptions of overridden methods from the base class in C++ 在子类的子类中使用基类C ++的虚拟方法 - use virtual method of base class C++ in child of child class C ++-如何使用基本指针覆盖子类方法 - C++ - How to override child class methods using base pointers 在转换为子类时将变量作为基本 class 对象迭代,并在 C++ 中使用它们的覆盖方法 - Iterate through variables as Base class objects while casting to Child classes and use their overriden methods in C++ 如何将基类 class 的未知子类放入一个数据结构中,并在 C++ 中调用覆盖的基类 class function - How to put unknown child classes of a base class in one data structure and call an overridden base class function in C++ 基类的函数没有在 C++ 中被覆盖 - Base class's functions aren't being overridden in C++ C ++,基类如何将子类作为成员包括在内并调用其方法 - C++, How can a Base class include a child class as a member and call its methods C++ 程序以 class 为基础,抽象 class 并派生 class,主要 function 使用所有相关数据和方法 - C++ program with base class, abstract class and derived class, in main function use all the related data and methods C ++:重叠基类方法 - C++: Overlapping base class methods C ++-从基类的运算符内部调用基类的重写虚拟方法 - C++ - calling a base class' overridden virtual method from inside base class' operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM