By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,515 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,515 IT Pros & Developers. It's quick & easy.

access the fields of a struct by position rather than name

P: n/a
J
Does anyone know how to do this in C++ or if it can be done?

And also to find out how many fields there are in a struct?
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"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
Jul 22 '05 #2

P: n/a

"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
Jul 22 '05 #3

P: n/a
"J" <jg******@hotmail.com> wrote:
Does anyone know how to [access the fields of a struct by position rather than name] in C++ or if it can be done?


This might not be what you are really asking, but there is a macro
offsetof(type, fieldname)
in <stddef.h> (<cstddef> ?)

This will only be useful to you if you know in advance the types of all the
fields, or if they
all have the same type.

You might be interested in the boost::Any template class, which can be used
to create
a heterogeneous list of values (ie.different types, just like a struct):

http://www.boost.org/doc/html/any.html
http://www.boost.org/doc/html/ch02s02.html (example of a list of boost::Any)

David F
Jul 22 '05 #4

P: n/a
J
>
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


This is what I am looking for but I was hoping C++ would have the
features built in, rather than having to extend the structures.

Have you heard of Tuples? Could something similar also be done with
Tuples?

J
Jul 22 '05 #5

P: n/a

"J" <jg******@hotmail.com> wrote in message
news:94**************************@posting.google.c om...

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


This is what I am looking for but I was hoping C++ would have the
features built in, rather than having to extend the structures.

Have you heard of Tuples? Could something similar also be done with
Tuples?

J


There's a very sophisticated Tuple implementation in the Loki library, it
allows any arbitary number and type of fields. Everything is worked out at
compile time instead of runtime like my crude code.

http://sourceforge.net/projects/loki-lib/

Boost also have a tuple library, don't know anything about that however.

http://www.boost.org/

john
Jul 22 '05 #6

P: n/a

"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.c om...

There's a very sophisticated Tuple implementation in the Loki library, it allows any arbitary number and type of fields. Everything is worked out at compile time instead of runtime like my crude code.

http://sourceforge.net/projects/loki-lib/

Boost also have a tuple library, don't know anything about that however.
http://www.boost.org/


Definitely learn the boost version, as it, or an extension, will
likely be in the next standard.

Jonathan
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.