| re: Pointer to class member array ... element? Possible?
Kaz Kylheku wrote:[color=blue]
> Given some class C with array T x[N], is it possible to get a
> pointer-to-data-member to one of the elements?[/color]
IIUIC, an element of the member array is not a member of that class, so,
no, it should not be possible.
[color=blue]
> &C::x gives us a pointer-to-member-array: T (C::*)[N].[/color]
Right.
[color=blue]
> 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.[/color]
Keep the index instead. Can't you just do c->x[indexyoukeep] ?
[color=blue]
> 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.[/color]
Bad assumption. Imagine what's going to happen when you have multiple
(or, which is worse, virtual) inheritance... Well, I don't actually know
if it's bad, but I just shy away from things like that.
V |