0
struct A
{
    struct one
    {
    int c;
    } details[MAX];
};

struct B
{
    struct two
    {
    int a;
    A::one b
    } details[MAX];
};

There is a validate function used like below to validate details array of struct in struct A:

bool ValidateOne(struct one * ptr)
{
    struct one * tmp[MAX];
    for (int i = 0; i < MAX; ++i) 
        tmp[i] = ptr+i;
    Validate(tmp[0]);
}

then there is validations needs to be done on details array of struct B: I expect to do:

bool ValidateTwo(struct two * ptr)
{
    struct one * tmp[MAX];
    for (int i = 0; i < MAX; ++i) 
        tmp[i] = &((ptr+i)->b);
    Validate(tmp[0]);
   //validate other stuff
};

Validate(struct one * ptrs[])
{
    int count;
    for (int i = 0; i < MAX; ++i) 
        count += ptrs[i]->a;
}

Would above code work?

4

1 回答 1

1

您可以定义一个迭代器类型,它知道如何到达数组中的下一项。"A::iterator" 将在一个简单的 "one" 数组上运行。"B::iterator" 将对 B::two 的数组进行操作。

通过这种方式,您不需要在任何一种情况下创建一个单独的指针数组,只需要迭代器的两个不同实现。

如果使用模板完成,则“ValidateOne()”也必须是模板。如果使用虚函数完成,则不需要模板。

于 2013-10-17T04:39:07.363 回答