Iterate through a struct

Jun 13, 2012 at 1:35pm
Hi,

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.

Regards
Last edited on Jun 13, 2012 at 1:43pm
Jun 13, 2012 at 3:07pm
The struct is dynamic and can have any number of members / elements of any data type.


Such a struct is impossible in C++ because C++ requires structs be fully and rigidly defined.

Can you post an example struct?
Jun 13, 2012 at 3:30pm
Are you talking about an array?
Jun 13, 2012 at 4:13pm
Thank you!

Here is the example @Disch

Example:

struct abc{
int i;
string j;
};

struct def{
double k;
string l;
int m;
};

abc ex1[2] = {{2, "new"}, {1, "one"}};
def ex2[1] = {2.2, "new", 10};

func1(ex1)
func1(ex2)
--------------------------------
void func1(struct example)
{
Iterate through the struct variables/elements
}
--------------------------------

The above is what I am trying to do..

Could you please suggest something else if what I am trying is impossible in C++.

@Flurite: I'm working with Structs (because if I use an array I can't have multiple data types) and my requirement is to have multiple datatypes.

Jun 13, 2012 at 6:26pm
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':

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
struct GeneralStruct
{
  virtual void 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();
}
Jun 13, 2012 at 6:54pm
Thank you for responding :)

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

Is there a way to accomplish this task?

Jun 13, 2012 at 7:02pm
Or you could overload func1() for every struct you have.
Last edited on Jun 13, 2012 at 7:05pm
Jun 13, 2012 at 7:06pm
Thank you!

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?
Jun 13, 2012 at 7:36pm
Maybe boost::any in a std::vector would serve you.
Just brainstorming
Jun 13, 2012 at 7:56pm
Thank you!
Could you please explain with a simple example.
Jun 13, 2012 at 8:19pm
Jun 13, 2012 at 8:22pm
Thank you! Will try that tomorrow and will let you know :)

Regards
Jun 14, 2012 at 7:13am
I have one other question.

If all the structs I make have the same name but different datatype of elements , can I pass the struct to the function for ex. to func1() ?
Jun 14, 2012 at 7:15am
I can try it myself to check it before I make use of boost::any
Topic archived. No new replies allowed.