I have a data structure that I want to access / modify in different ways in different situations. I came up with this:
class DataStructure
{
public:
int getType();
private:
// underlying data containers
};
class WrapperBase
{
public:
void wrap(DataStructure *input)
{dataStructure = input;}
protected:
DataStructure *dataStructure;
};
class WrapperOne : public WrapperBase
{
public:
// @Mykola: I know bytes 4-10 in an array of type one specify the date
// so this method will format those bytes and return them
Data getDate()
};
class WrapperTwo : public WrapperBase
{
public:
// @Mykola: There is mostly just a chunk of data in packet type two. So this
// method will turn 4 bytes into an int at position index and return that
int dataAt(int index);
};
One problem I see here is that WrapperBase isn't abstract even though it should be. I could of course add some pure virtual dummy function or an assert(0) in the constructor but that seems too hackish a solution. Or should I get rid of the inheritance entirely since it's only really done for code-reuse? Any other problems with this solution?
Or am I on the wrong track entirely?
Edit @ Paul
Why do I want to do this? Well, I get several 1000 arrays of serialized data, which I want to add to a dataset. The first few bytes of each array tell me what sort of data it is, which dictates how I have process it. So then I do something like:
// some place in the program
dataSet.processData(dataStructure);
// in the data set class
DataSet::processData(DataStructure *dataStructure)
{
if(dataStructure->getType() == TYPE_ONE)
{
WrapperOne wrapperOne;
wrapperOne.wrap(dataStructure);
processDataTypeOne(wrapperOne);
}
// repeat the above for other data types
}
I could of course put all the logic in the processDataTypeOne
function, and that was what I was doing in the beginning, but operating on the raw data structure turned into an ugly mess of index operations. That's why I'd like to wrap it in an object, which will hide all that.