K
Kaz Kylheku
Given some class C with array T x[N], is it possible to get a
pointer-to-data-member to one of the elements?
&C::x gives us a pointer-to-member-array: T (C::*)[N].
But I just want to get a T C::* pointing to a selected array element,
so that I can later use an instance c of that class to pick out that
array element: c->*ptr.
This syntax, for instance, doesn't work: &C::x[1]. My compiler thinks
I'm trying to do scope resolution to look up x as a member of some base
class: it complains that C isn't a base class of the class that I'm
working in. Of course &C::x works fine. How to sneak in that array
reference? (&C::x)[1] isn't it because then you are doing array
indexing on a pointer to the array, which is invalid; you need an
object to do that.
Is there any way? All I have is this hack:
T (C::*pma)[N] = &C::x; // pointer to member array
T C::*pe1 = (T C::*) ((size_t) pma + sizeof T);
The assumption is that the pointer-to-member is an integer-like offset
that can be converted to a size_t, subject to displacement by a
multiple of the array size and the converted back to pointer-to-member.
pointer-to-data-member to one of the elements?
&C::x gives us a pointer-to-member-array: T (C::*)[N].
But I just want to get a T C::* pointing to a selected array element,
so that I can later use an instance c of that class to pick out that
array element: c->*ptr.
This syntax, for instance, doesn't work: &C::x[1]. My compiler thinks
I'm trying to do scope resolution to look up x as a member of some base
class: it complains that C isn't a base class of the class that I'm
working in. Of course &C::x works fine. How to sneak in that array
reference? (&C::x)[1] isn't it because then you are doing array
indexing on a pointer to the array, which is invalid; you need an
object to do that.
Is there any way? All I have is this hack:
T (C::*pma)[N] = &C::x; // pointer to member array
T C::*pe1 = (T C::*) ((size_t) pma + sizeof T);
The assumption is that the pointer-to-member is an integer-like offset
that can be converted to a size_t, subject to displacement by a
multiple of the array size and the converted back to pointer-to-member.