get a struct member pointer
Christopher Wright
dhasenan at gmail.com
Mon Feb 16 04:51:40 PST 2009
Jeffry Nox wrote:
> struct A {
> uint id=0;
> char[] name;
> }
>
> struct B {
> uint id=0;
> char[] title;
> }
>
> void lookup(T)(T[] s, ***)
> {
> char[] txt = s[0].***;
> }
>
> as illustrated above, how can i get struct object property info as in *** so i can manipulate it inside the function lookup above? in function lookup, i didnt know what is the variable name for the char[], so *** pass in the 2nd parameter, question is how to pass that member info or pointer so that it works on different struct declarations?
>
Use tupleof. You need to specify a compile-time constant value as the
index to tupleof, but looping also works:
import tango.io.Stdout;
struct S
{
int i;
char[] b;
}
void getItem (S s, int index)
{
foreach (i, n; s.tupleof)
{
Stdout.formatln("{} {}", i, typeid(typeof(s.tupleof[i])));
if (i == index)
{
auto value = s.tupleof[i];
Stdout.formatln("Value is `{}`", value);
}
}
}
void main (char[][] args)
{
S s;
getItem(s, 0);
getItem(s, 1);
}
More information about the Digitalmars-d-learn
mailing list