简体   繁体   English

如何将基类 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++

I have a "chain" class, meaning a class that manages a sequence of objects with a common base class. This chain class should execute the member function processSample(a, b) for any child class (of baseClass ) I add.我有一个“链”class,意思是一个 class,它管理一个具有公共基数 class 的对象序列。这个链 class 应该为任何子对象 8839818195188 执行成员 function processSample(a, b) baseClass

I want to be able to code more child classes (with a processSample(a, b) function) later on, and add them to the chain without having to edit the chain class. I could use a template in the add function but this doesn't solve the problem that there is no data structure for different datatypes (of different sizes) right?我希望稍后能够编写更多子类(使用processSample(a, b)函数),并将它们添加到链中而无需编辑链 class。我可以在add function 中使用模板,但这不会'解决了没有针对不同数据类型(不同大小)的数据结构的问题吧?

Functions of the children called in the Chain class should all be overridden virtuals from the base class.Chain class 中调用的子函数都应该被基 class 的虚拟函数覆盖。

class baseClass
{
public:

    virtual float processSample(int a, float b)
    {

    }
};


class Chain
{
    const int maxChilds = 20;
    ?sometype? allChilds[maxChilds];

public:
    float processSample(int c, float d)
    {
        for (int i = 0; i < maxChilds; i++)
        {
            input = allChilds[i].processSample(a, b);
        }

        return input;
    }

    void addChild(?sometype? newChild)
    {
       allChilds.push_back(newChild)
    }
}

You would want to use pointers or references, to avoid initialization or copy operations as part of creating the class.您可能希望使用指针或引用,以避免初始化或复制操作作为创建 class 的一部分。

For example例如

class Chain
{
    const int maxChilds = 20;
    baseClass allChilds[maxChilds];

Will create, and initialize an array of 20 baseClass instances.将创建并初始化一个包含 20 个 baseClass 实例的数组。 Whereas this:而这个:

class Chain
{
    const int maxChilds = 20;
    baseClass* allChilds[maxChilds];

Will create an array of pointers to baseClass, which can also point to instances of any child classes.将创建一个指向 baseClass 的指针数组,它也可以指向任何子类的实例。 Do consider which class will be responsible for allocation and de-allocation of the memory for these instances (this can either be within this class, or the caller / user of this class - which one is ideal will depend on the rest of your design).请考虑哪个 class 将负责这些实例的 memory 的分配和取消分配(这可以在这个 class 中,或者这个 class 的调用者/用户 - 哪个是理想的将取决于您设计的 rest) . You could also consider using a smart pointer instead and let that manage the memory for you: https://en.cppreference.com/book/intro/smart_pointers您也可以考虑改用智能指针,让它为您管理 memory: https://en.cppreference.com/book/intro/smart_pointers

Similarly:相似地:

void addChild(baseClass newChild)

Will use pass-by-value to pass in a copy of newChild (using the copy constructor of baseClass), any instances of child classes will either fail or be converted to an instance of baseClass.将使用传值方式传入 newChild 的副本(使用 baseClass 的复制构造函数),子类的任何实例要么失败,要么被转换为 baseClass 的实例。 Whereas if you instead go with:而如果您将 go 改为:

void addChild(baseClass& newChild)

It will use pass-by-reference instead, and the function will receive a reference to the original object.它将改用引用传递,function 将收到对原始 object 的引用。

The nice effect of virtual is, that you "call a function of the base class", but the function of the derived class will be executed. virtual 的好处是,你“调用基类的 function”,但是派生的 class 的 function 将被执行。

So ?sometype? allChilds[maxChilds];那么?sometype? allChilds[maxChilds]; ?sometype? allChilds[maxChilds]; is baseClass allChilds[maxChilds];baseClass allChilds[maxChilds]; . . The chain should be the holder of the objects, so addChild(...) should not accept an instance of the child-class.链应该是对象的持有者,所以addChild(...)不应该接受子类的实例。 It should create the instance and use (if necessary) std::move to add it to the array.它应该创建实例并使用(如有必要) std::move 将其添加到数组中。

Remark: It will be easier for you using备注:使用起来会更方便

std::vector<baseClass>allChilds;
allChilds.reserve(maxChilds); //reserves the memory but you can still use push back

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM