I am trying to store some data that I read from a file. The data is an array and can be one of a few types; short, float, etc. I am trying to abstract this data to a class, so I immediately thought:
class BaseData {
public:
void *data;
virtual void foo() = 0;
};
template <typename T>
class Data : public BaseData {
public:
T *data;
Data(const File *file) {
data = (T*) file->data;
}
T operator()(int x, int y, int z) {
return data[x + y*yd + z*zd];
}
void foo() {}
};
I use a switch statement when reading the file to instantiate the right object and then when I want to work with the different Data objects I do:
if (Data<short> *d = dynamic_cast<Data<short> *>(image->data)) {
cout << (*d)(100, 50, 100) << endl;
}
where image->data is of type BaseData.
What are your thoughts on this? Is this the right way to go about things? Ideally I would like to grab the array (and the type) and do things with it, but I don't know what the type is going to be at compile time.
T *datainclass Data, you're hidingvoid *datadefined inclass BaseData- the void pointer will never be used.