简体   繁体   English

从指向基类对象的指针向量调用派生类的唯一功能

[英]Invoke unique functions of derived classes from a vector of pointers to base class objects

#include <iostream>
#include <vector>

using namespace std;

class Base
{
public:
    void speak() { cout << "Hello!"; }
};

class Derived1 : public Base
{
public:
    void func1() { cout << "I'm a function!"; }
};

class Derived2 : public Base
{
public:
    void func2() { cout << "I'm also a function!"; }
};

int main()
{
    vector<Base*> v = { new Derived1(), new Derived2(), new Derived1(), new Derived2() //, ...
    };
    // For each Derived1 object invoke func1() and for each Derived2 object invoke func2()
}

Base is not polymorphic (no virtual function). Base不是多态的(没有虚函数)。 From these conditions how can I be able to invoke func1 for each Derived1 object and func2 for each Derived2 object in v? 从这些条件出发,我如何能够为v中的每个Derived1对象调用func1并为每个Derived2对象调用func2?

"Base is not polymorphic (no virtual function)." “基数不是多态的(没有virtual函数)。”

You still can do a static_cast<Derived1>(v[0]) / static_cast<Derived2>(v[1]) , aso, as long you're sure what you'll get at a particular index. 仍然可以执行static_cast<Derived1>(v[0]) / static_cast<Derived2>(v[1]) ,只要您确定在特定索引下会得到什么。

As-is, it's impossible. 照原样,这是不可能的。 You can't do dynamic_cast , no member that indicates one type or the other. 您不能执行dynamic_cast ,没有成员可以指示一种类型或另一种类型。 There's absolutely no way to distinguish given a Base* whether it's a Derived1* or a Derived2* . 绝对没有办法区分给定的Base*Derived1*还是Derived2*

You would have to do one of: 您将必须执行以下一项操作:

  1. Add a virtual void func() = 0; 添加virtual void func() = 0; that the two different classes would override. 两个不同的类将被覆盖。

  2. Add a member variable of type std::function<void()> that would be set in the Base constructor differently by the two Derived classes. 添加一个类型为std::function<void()>的成员变量,该变量将在Base构造函数中由两个Derived类进行不同的设置。

  3. Add a member variable that's an enum to indicate which Derived it is, so that you could do a switch externally to do a safe static_cast . 添加一个成员变量,该变量是一个枚举,以指示它是哪个Derived的,以便您可以在外部进行切换以执行安全的static_cast

  4. Add a virtual ~Base() so that that instead of a switch, you would do dynamic_cast s. 添加一个virtual ~Base()以便您执行dynamic_cast而不是进行开关。

  5. Not actually have either Derived1 or Derived2 inherit from Base , but instead have a variant<Derived1, Derived2> . 实际上没有从Base继承Derived1Derived2 ,而是有一个variant<Derived1, Derived2>

  6. ??? ???

  7. Profit. 利润。

暂无
暂无

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

相关问题 从派生类访问成员函数的基类指针向量? 正确的方法? - Vector of base class pointers to access member functions from derived classes? The correct way? 将具有映射的抽象基类派生的对象存储在基类指针的向量数组中 - Storing a objects derived from an abstract base class with maps in a vector array of base class pointers 如何在指向基类对象的指针向量中引用派生对象? - How to reference derived objects in a vector of pointers to base class objects? 制作基类指针的向量并将派生类对象传递给它(多态) - Making a vector of base class pointers and pass Derived class objects to it (Polymorphism) 指向包含派生类对象和重载&lt;&lt;运算符的基类的指针数组 - Array of pointers to the base class that contains objects of derived classes and overloaded << operator 指向包含基类和派生类 class 对象的指针的向量 - 访问派生类特定变量 - Vector of pointers to base class containing base and derived class objects - accessing derived-class specific variables 创建一个带有 6 个指针的向量(3 个来自基础 class,3 个来自派生类) - Creating a vector with 6 pointers(3 from base class and 3 from derived class) 如何在指向具有派生类实例的基类的指针向量上实现派生类的副本构造函数? - How to implement a copy constructor of the derived class on a vector of pointers to base class which has instances of derived classes? 指向矢量的基类在派生类中 - Vector of pointers to base class within a derived class 使用基类指针派生类对象 - Using Base Class Pointers to Derived Class Objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM