简体   繁体   English

C++:调用派生的class的虚拟function

[英]C++: Calling the virtual function of the derived class

Suppose I have a class with a virtual function and a derived class that implements the virtual function in a different way.假设我有一个 class 和一个虚拟 function 和一个派生的 class 以不同的方式实现虚拟 ZC1C425268E68384D14。 Suppose I also have a vector of the base class used to store derived classes.假设我还有一个用于存储派生类的基本 class 的向量。 How would I execute the virtual function of a derived class in the vector without knowing in advance what the derived class is?如果事先不知道派生的 class 是什么,我将如何在向量中执行派生 class 的虚拟 function ? Minimal code that illustrates the problem:说明问题的最少代码:

#include <iostream>
#include <vector>

class Foo {
public:
    virtual void do_stuff (void) {
        std::cout << "Foo\n";
    }
};

class Bar: public Foo {
public:
    void do_stuff (void) {
        std::cout << "Bar\n";
    }
};

int main (void) {
    std::vector <Foo> foo_vector;
    Bar bar;

    foo_vector.resize (1);
    foo_vector [0] = bar;

    bar.do_stuff ();            /* prints Bar */
    foo_vector [0].do_stuff (); /* prints Foo; should print Bar */
    return 0;
}

You can't.你不能。 The objects in the vector will have been sliced -- any derived-class instance data will have been chopped off, so calling the method would be a super-bad idea.向量中的对象将被切片——任何派生类实例数据都将被切掉,因此调用该方法将是一个非常糟糕的主意。

If, on the other hand, you have a vector of pointers to base, then you simply call the virtual method, and the derived-class version will be invoked.另一方面,如果你有一个指向基的指针向量,那么你只需调用虚方法,派生类版本就会被调用。

The class that you are actually calling is not a class of Bar, but instead a class of Foo.您实际调用的 class 不是 Bar 的 class,而是 Foo 的 class。 What you are doing at foo_vector [0] = bar;你在foo_vector [0] = bar; is a call of the implicit operator= which is doing its best to make something smart happen.是隐式 operator= 的调用,它正在尽最大努力使一些聪明的事情发生。 The memory space is still the size of a Foo though, so it can't ever be a Bar. memory 空间仍然是 Foo 的大小,所以它永远不可能是 Bar。

When using virtual functions, you make use of pointers to objects.使用虚函数时,您会使用指向对象的指针。 This way the exact function is called during run time ("How would I execute the virtual function of a derived class in the vector without knowing in advance what the derived class is?" What you mean here probably is "run time"). This way the exact function is called during run time ("How would I execute the virtual function of a derived class in the vector without knowing in advance what the derived class is?" What you mean here probably is "run time").

The use of marking a function as virtual is that, you ask the compiler to postpone or figure out the "TYPE" of an object calling that function at run time rather than the usual way "compile time".将 function 标记为虚拟的用途是,您要求编译器推迟或找出 object 的“类型”,在运行时调用 function 而不是通常的“编译时”方式。 This is achieved by using pointers to objects.这是通过使用指向对象的指针来实现的。 So to put it in a simple line "Use pointers to objects to make use of Virtual functions".因此,简单地说“使用指向对象的指针来使用虚函数”。

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

相关问题 从 c++ 中派生的 class 的析构函数调用虚拟 function - Calling a virtual function from a destructor of a derived class in c++ C ++派生类-从父类调用虚函数? - C++ Derived Classes - Calling a virtual function from the parent class? How a friend function of a base class is calling the overridden virtual function of derived class even though they are private?(C++) - How a friend function of a base class is calling the overridden virtual function of derived class even though they are private ?(C++) C ++继承:使用派生类的实例调用基类的虚函数 - C++ Inheritance: Calling the virtual function of a base class using an instance of a derived class 在派生类中调用虚函数 - Calling virtual function in derived class C ++在派生类指针数组上调用虚拟函数 - C++ Calling virtual functions on array of derived class pointers C++虚函数:调用基类函数而不是派生函数 - C++ virtual function: Base class function is called instead of derived 在派生类C ++中避免“纯虚函数调用” - Avoiding “Pure Virtual Function Call” in Derived Class C++ 访问 C++ 中派生的 class 的模板参数的虚拟成员 function - Accessing a virtual member function of a template argument derived class in C++ 如果虚函数引用C ++中派生类中的变量,这是安全的吗? - Is it safe if a virtual function refers to variable in a derived class in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM