简体   繁体   English

将一组Child对象传递给接受Parent *的函数

[英]Passing an array of Child objects to a function that accepts Parent*

I am working on an embedded platform with limited capabilities, so vectors/STL are not available. 我正在使用功能有限的嵌入式平台,因此无法使用矢量/ STL。

This may be a trivial problem, but I do not have much experience in C++ (only C and C#, which may make me blind to an obvious c++ way to do it). 这可能是一个微不足道的问题,但我没有太多的C ++经验(只有C和C#,这可能让我对显而易见的c ++方法视而不见)。

Consider the following example: 请考虑以下示例:

class Parent {
};

class Child : public Parent {
};

void Test(Parent* parents, uint8_t parentCount) {
    // Accessing parent[x] is problematic when 'parents' contains a derived type
}

int main() {
    // This is OK
    Parent parents[3];
    Test(parents, 3);

    // This causes problems
    Child children[3];
    Test(children, 3);

    return 0;
}

Obviously it is problematic to iterate over parents in Test() , if a pointer to an array of derived classes is provided, because the memory footprint of Parent is assumed during the iteration. 显然,如果提供了指向派生类数组的指针,则在Test()中迭代类是有问题的,因为在迭代期间假定Parent的内存占用。

The only solution I see is to pass an array of pointers of type Parent (Parent** parents), but that seems cumbersome. 我看到的唯一解决方案是传递一个类型为Parent (Parent ** parents)的指针数组,但这看起来很麻烦。 Is there some C++ mechanism I am not aware of, like passing the array as a reference or something? 是否有一些我不知道的C ++机制,比如将数组作为引用或其他东西传递?

You could use this approach: 你可以使用这种方法:

template <class T>
void Test(T* parents, uint8_t parentCount) {
    // Code that accesses parent[x]
}

and then use it like this: 然后像这样使用它:

int main() {

    Parent parents[3];
    Test(parents, 3);

    Child children[3];
    Test(children, 3);

    return 0;
}

If template is not an option and when the user of Test can not depend on Child and can't even know it's size, then you can use an array of pointers: 如果模板不是一个选项,并且Test的用户不能依赖于Child而且甚至不知道它的大小,那么你可以使用一个指针数组:

void Test(Parent** parents, uint8_t parentCount);

int main() {
    Child children[n];
    Child* pointers[n];
    for(int i = 0; i < n; i++)
        pointers[i] = &children[i];
    Test(pointers);
}

Note that in this trivial example, we do know the size of the object whose pointers we pass, but in general, we may not be able to make that assumption for all users of Test . 注意,在这个简单的例子,我们知道它的指针,我们通过对象的大小,但在一般情况下,我们可能不能够做这样的假设为所有用户Test

If you can't use templates, you can do this: 如果您无法使用模板,则可以执行以下操作:

class Parent {
};

class Child : public Parent {
};

void Test(Parent* parents, uint8_t parentCount, uint16_t parentSize) {
    for (uint8_t ii = 0; ii < parentCount; ++ii) {
        void* parentvoid = reinterpret_cast<char*>(parents) + ii * parentSize;
        Parent* parent = parentvoid;
    }
}

int main() {
    Parent parents[3];
    Test(parents, 3, sizeof(parents[0]));

    Child children[3];
    Test(children, 3, sizeof(children[0]));
}

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

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