Re: access the fields of a struct by position rather than name
"J" <jgaspara@hotma il.com> wrote in message
news:94011fe6.0 402080412.58127 67@posting.goog le.com...[color=blue]
> Does anyone know how to do this in C++ or if it can be done?[/color]
It can be done with very crude casting and pointer arithmetic, but its
definitely a bad idea.
[color=blue]
>
> And also to find out how many fields there are in a struct?[/color]
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.
Re: access the fields of a struct by position rather than name
"John Harrison" <john_andronicu s@hotmail.com> wrote in message
news:c05d0d$13b t8m$1@ID-196037.news.uni-berlin.de...[color=blue]
>
> "J" <jgaspara@hotma il.com> wrote in message
> news:94011fe6.0 402080412.58127 67@posting.goog le.com...[color=green]
> > Does anyone know how to do this in C++ or if it can be done?[/color]
>
> It can be done with very crude casting and pointer arithmetic, but its
> definitely a bad idea.
>[color=green]
> >
> > And also to find out how many fields there are in a struct?[/color]
>
> 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
>[/color]
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.
Re: access the fields of a struct by position rather than name
"J" <jgaspara@hotma il.com> wrote:
[color=blue]
> Does anyone know how to [access the fields of a struct by position rather[/color]
than name][color=blue]
> in C++ or if it can be done?[/color]
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):
Re: access the fields of a struct by position rather than name
>[color=blue]
> 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[/color]
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?
Re: access the fields of a struct by position rather than name
"J" <jgaspara@hotma il.com> wrote in message
news:94011fe6.0 402100003.4d12b 4ef@posting.goo gle.com...[color=blue][color=green]
> >
> > 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[/color]
>
> 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[/color]
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.
Re: access the fields of a struct by position rather than name
"John Harrison" <john_andronicu s@hotmail.com> wrote in message
news:c0a3ss$14p 8st$1@ID-196037.news.uni-berlin.de...[color=blue]
>
> "J" <jgaspara@hotma il.com> wrote in message
> news:94011fe6.0 402100003.4d12b 4ef@posting.goo gle.com...[color=green][color=darkred]
> > >[/color][/color][/color]
[color=blue]
>
> There's a very sophisticated Tuple implementation in the Loki[/color]
library, it[color=blue]
> allows any arbitary number and type of fields. Everything is worked[/color]
out at[color=blue]
> 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[/color]
however.[color=blue]
>
> http://www.boost.org/
>[/color]
Definitely learn the boost version, as it, or an extension, will
likely be in the next standard.
Comment