I am trying to read data from file and store it into structures. These structures are quite similar, but for a couple of fields. As an example:
Code:
typedef struct
{
int a;
int b;
char c;
} STRUCT_A;
typedef struct
{
int a;
int b;
float d;
} STRUCT_B
I use STRUCT_A to make a list of such objects, which are collected in another structure (OVERSTRUCT_A). The same goes on for STRUCT_B and OVERSTRUCT_B (I actually have 4 of such structures, 2 of the used in one OVERSTRUCT and the other 2 in the other, but I'll keep the example simple).
Code:
typedef struct
{
STRUCT_A* pA;
} OVERSTRUCT_A;
typedef struct
{
STRUCT_B* pB;
} OVERSTRUCT_B;
Everything is really similar, and since the function that fills the lists of STRUCT_A and STRUCT_B contained in OVERSTRUCT_A and OVERSTRUCT_B respectively is so elaborated, I would like to keep this templetized, so that managing the code is easier (or I would have to type manually every modification twice).
The function looks like this:
Code:
template <typename T, typename R>
int func (T* STRUCT, R& OVERSTRUCT)
{ /* what I return is irrelevant in this example */ }
In the end, all the function does is work with a list, STRUCT is the head of one of the lists (type either STRUCT_A or STRUCT_B) and OVERSTRUCT is the struct containing the head of the list itself (type OVESTRUCT_A or OVERSTRUCT_B respectively).
I need to pass OVESTRUCT because the function needs to conceal any work done on the head of the list (which is automatically ordered while data is read from file).
Since I have more than 2 STRUCT_X types, it is necessary to pass STRUCT separately from OVERSTRUCT, to specify which one of the STRUCT in the OVERSTRUCT I want to work with.
I tried:
Code:
if (typeid (T) == typeid (STRUCT_A)) {OVERSTRUCT.pA}
if (typeid (T) == typeid (STRUCT_B)) {OVERSTRUCT.pB}
But this is an error since when OVESTRUCT == OVERSTRUCT_A, the compiler rightly argue that there is no field pB in OVERSTRUCT_A.
Aside from doing something like this:
Code:
template <typename T>
T* func (T* STRUCT)
{/* the head of the list may change, but the scope of STRUCT is inside func, thus I pass the pointer to the new head by returning by value */}
int main ()
{
...
STRUCT_A.pA = func (pA);
STRUCT_B.pB = func (pB);
...
}
How can I solve the problem alternatively?
I tried to explore the option of passing a pointer by reference, but people try to discourage this (I'm not sure why, though). Any other ideas?
Bookmarks