"John Harrison" <jo*************@hotmail.com> wrote in message

news:c0*************@ID-196037.news.uni-berlin.de...

"J" <jg******@hotmail.com> wrote in message

news:94*************************@posting.google.co m... Does anyone know how to do this in C++ or if it can be done?

It can be done with very crude casting and pointer arithmetic, but its

definitely a bad idea.

And also to find out how many fields there are in a struct?

That cannot be done.

Almost certainly you are trying the wrong approach, either you need to

change your approach or you need to change your language. Why not explain

what you are actually trying to achieve and better advice can be given.

john

Perhaps something like this, I'm assuming that all your fields are int's

struct Base

{

virtual ~Base() {}

virtual int num_fields() = 0;

virtual int get_field(int n) = 0;

virtual void set_field(int n, int val) = 0;

};

struct Pair : public Base // a struct with two fields

{

virtual int num_fields() { return 2; }

virtual int get_field(int n)

{

if (n == 0)

return first;

else if (n == 1)

return second;

else

error("not enough fields");

}

virtual void set_field(int n, int val)

{

if (n == 0)

first = val;

else if (n == 1)

second = val;

else

error("not enough fields");

}

int first;

int second;

};

That's how you'd do it in C++, if you really have to do it at all.

john