我有一个多个类继承的接口。
class someInterface
{
virtual void someMethod() = 0;
}
class A : public someInterface
{
public:
void someMethod()
{
//Do something
}
}
class B : public someInterface
{
public:
void someMethod()
{
//Do something
}
}
class C : public someInterface
{
public:
void someMethod()
{
//Do something
}
}
对于 A、B、C 类中的每一个,我在容器类中创建了一个具有不同大小的实际类型的数组。
class AContainer
{
public:
A As[10];
}
class BContainer
{
public:
B Bs[5];
}
etc...
此外,我有一个指向“SomeInterface”的指针数组,我想在其中有一个指向每个像这样的实际数组的指针。
#define SOMEINTERRFACE_SIZE 3
someInterface *array[SOMEINTERRFACE_SIZE];
array[0] = AContainer.As; //Could also just be &AContainer.As[0]
array[1] = BContainer.Bs;
array[2] = CContainer.Cs;
for (int i = 0; i < SOMEINTERRFACE_SIZE; ++i)
{
int elements = //Here i need a solution to get the size
//So i can iterate through the array, which the pointer points to.
for (int i = 0; i < elements; ++i)
{
//Call the interface method on each element.
}
}
当我必须使用 someInterface 数组时,就会出现问题,因为无法通过 someInterface 指针获取实际数组的大小。
这个问题有什么好的解决方案?我真的需要一些帮助来解决这个问题。也不想使用动态分配,所以没有使用 vector<> 或 malloc 等的解决方案,因为我正在写信给 Arduino。