提供派生对象的数组上基本对象操作功能 [英] Provide array of derived objects to function that operates on base objects

查看:152
本文介绍了提供派生对象的数组上基本对象操作功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找适当的标题,这个问题对我来说有点困难,但这里是我的处境。说我有一个基类和派生类,我可以通过一个函数,需要一个类型,而不是键入*基地的参数*衍生

Finding the appropriate title to this question is a little difficult for me, but here is my situation. Say I have a base class and a derived class, can I pass a function that takes a parameter of type *base a type of instead *derived


class base
{
protected:
    float M_OutputValue;
    float M_InputValue;
public:
    virtual void CalculateOutput() = 0;
        /* more functions */
};

class derived: public base
{
public:
    void CalculateOutput(){ /*stuff*/};
    /* more functions */
};

这是将采取基类作为输入的功能。

And here is a function that would take the base class as an input.



void CalculateAllOutputs(base* basearray, int NumberofElements)
{
    int temp = 0;
    while(temp < NumberOfElements)
    {
         basearray[temp]->CalculateOutput();
         temp++;
    }
};

我和想知道如果我能一个指针传递此功能派生的对象的数组。据我了解有关源自于基铸造指针,它是隐含的,没有什么可担心的(我看了<一个href=\"http://stackoverflow.com/questions/2156064/can-a-pointer-of-a-derived-class-be-type-cast-to-the-pointer-of-its-base-class\">this答案的问题)。我担心的是,由于派生类中有更多的成员,需要更多的字节。这将导致构件的阵列中是相隔较远,所以通过它迭代时,更大的偏移将需要被添加到存储器地址,以获得下一个元素。我想知道如果编译器利用了这些问题,关心,这样的功能,我传递给知道如何访问派生数组的成员的数组。我还可以解决我的问题模板的这里没有解决办法,但我想在获得更好的多态性

I'm and wondering if I can pass this function a pointer to an array of derived objects. From what I understand about casting pointer from derived to base, it is implicit and there isn't much to worry about (I looked at this question for answers). My concern is that because the derived class has more members, it takes up more bytes. That would cause the members in the array to be farther apart, so when iterating through it, a larger offset would need to be added to the memory address to get to the next element. I'm wondering if the compiler takes care of these problems so that the function I'm passing the array to knows how to access the members of the derived array. I could also solve my problem with templates of there is no solution here, but I'm trying to get better at polymorphism.

感谢。

推荐答案

您是对的。远离阵列的路程,preferably使用

You are right. Stay away from arrays, and preferably use

std::vector<std::unique_ptr<base>>

或者如果你没有的C ++ 0x就绪编译器(如果是这样的话,再考虑升级),你可以使用

or if you don't have a C++0x-ready compiler (if this is the case, then consider upgrading), you can use

boost::ptr_vector<base>

两者都是指针自动清理所指向的对象的矢量。避免生指针,正如你指出,普通阵列简单地吸吮。

Both are vectors of pointers which automatically clean up the pointed to objects. Avoid raw pointers, and as you point out, plain arrays simply suck.

使用的示例:

void calculate_outputs(const std::vector<std::unique_ptr<base>>& b)
{
    std::for_each(b.begin(), b.end(), [](const std::unique_ptr<base>& x)
    {
        x->CalculateOutput();
    });
}

这篇关于提供派生对象的数组上基本对象操作功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆