How to use different structure elements generically inside a function

Hi,

I want to be able to pass variables pointing to different data structures, as arguments to a finction and have the function do various operations on the elements of the data structure, without having to write the same function for each data structure type.

For example, I might have:

1
2
3
4
5
6
7
8
9
10
11
12
struct s1 {
 char *string1;
 int var1;
};

struct s2 {
 long var2;
 long var3;
};

s1 v1;
s2 v2;


I would like to call a function foo, where

1
2
3
4
5
6
7
8
9
void foo(S v) {
.....

//In some parts I would like to access v1.string1 and process it.
//In some parts I would like to access v2.var2 and process it....etc.

.....

}


The simple solution is by using a switch statement, but is there a more elegant solution that uses templates, void pointers, etc.?
Research polymorphism.
Topic archived. No new replies allowed.