S
Steve Rencontre
I can't for the life of me see how to do pointer-to-member when the
member is actually part of an embedded structure.
That is, if I have:
struct S1 { int a; };
struct S2 { S1 s; int b; };
how can I get a pointer to the a in an S2?
Obviously I can point at the b with
int S2:*p = &S2::b;
But the seemingly obvious syntax
int S2::*p = &S2::s.a;
doesn't work - because S2::s.a is not a /qualified-id/ - and neither
does anything else I can think of.
After all, if I have an S2 structure, there's absolutely no difference
from the compiler's POV between s2.s.a and s2.b, so why should I not be
able to have a construct s2.*p equivalent to the former case?
At the moment, the only thing I have found is a rather yucky workaround
with offsetof() macros and lots of casts. That is,
size_t offset = offsetof (S2, s.a);
*(int *) ((char *) &s2 + offset) = 123;
Ugh :-(((
Any better suggestions gratefully received!
member is actually part of an embedded structure.
That is, if I have:
struct S1 { int a; };
struct S2 { S1 s; int b; };
how can I get a pointer to the a in an S2?
Obviously I can point at the b with
int S2:*p = &S2::b;
But the seemingly obvious syntax
int S2::*p = &S2::s.a;
doesn't work - because S2::s.a is not a /qualified-id/ - and neither
does anything else I can think of.
After all, if I have an S2 structure, there's absolutely no difference
from the compiler's POV between s2.s.a and s2.b, so why should I not be
able to have a construct s2.*p equivalent to the former case?
At the moment, the only thing I have found is a rather yucky workaround
with offsetof() macros and lots of casts. That is,
size_t offset = offsetof (S2, s.a);
*(int *) ((char *) &s2 + offset) = 123;
Ugh :-(((
Any better suggestions gratefully received!