我必须将几兆字节的数据放在 C++ 代码中的二维数组中(将其嵌入到 DLL 中),每个子类都有不同的数据集。我定义了虚拟访问器方法来访问指定子类的常量,但它仅适用于基元和一维数组,不适用于二维数组:
#include <stdio.h>
class SubClassHoldingData { // inheritance removed for short,compilable example
public:
static int const notWorkingData[2][2];
virtual int const** getNotWorkingData() { return (int const**)notWorkingData; }
};
// simplified data set, about 200x200 in real application
const int SubClassHoldingData::notWorkingData[2][2] = { { 1 , 2 } , { 3, 4 } };
int main( int argc , char** argv ) {
SubClassHoldingData* holder = new SubClassHoldingData();
const int** data = holder->getNotWorkingData();
printf("data: %d" , data[1][1]); // !!! CRASHES APPLICATION !!!
}
我想访问动态数据(虚拟),但使用编译时常量数组,如下所示:
DataHolder* holder = new FirstDataSetHolder();
const int** data = holder->get2DArray();
DataHolder* holder = new SecondDataSetHolder();
const int** data = holder->get2DArray();
// "data" contents DIFFERENT now, but compile-time constants!
如何做到这一点?