I'm trying to iterate through a struct which is passed to a function.
The struct is dynamic and can have any number of members / elements of any data type.
Is there a way to iterate though such kind of structs?
Please let me know if i'm unclear with the question.
Yes, this is not possible in C++. You can't make a function take a 'struct', you have to tell it what kind of struct (ie, you have to specify either abc or def, you can't just have it take any struct).
A possible solution to this is to use polymorphism instead of having a general function like 'func1':
struct GeneralStruct
{
virtualvoid IterateElements() = 0;
};
struct abc : public GeneralStruct
{
int i;
string j;
void IterateElements()
{
// iterate over abc's elements here
}
};
struct def : public GeneralStruct
{
double k;
string l;
int m;
void IterateElements()
{
// iterate over def's elements here
}
};
int main()
{
ex1[0].IterateElements();
}
Can I make use of some other container (list, map .. or anything else) to have multiple data types and pass it to the function?
Requirement is :
I get data from database as rows and each row has columns of different datatypes.
I will have to compare this data with the expected results.
The expected results are in the array of struct.
Now, I have to pass this array of struct to the function where I can compare the expected results with database values.
Note: I compare data from various tables, so I make a new struct of expected results according to the table I access and pass the new array of struct to function where I can compare the results
I'm trying to generalize func1() so that it can be used by the team very easily and the database is too big .. we will be doing the comparision on most of the tables .. I donot think it could be feasible to have so many overloaded functions for func1()
or is it possible to make overloaded method dynamically?