Connecting Tech Pros Worldwide Forums | Help | Site Map

Pointer to class member array ... element? Possible?

Kaz Kylheku
Guest
 
Posts: n/a
#1: Oct 7 '05
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.


mlimber
Guest
 
Posts: n/a
#2: Oct 7 '05

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?
>
> &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.[/color]

Please post a minimal but complete sample program.

Cheers! --M

Victor Bazarov
Guest
 
Posts: n/a
#3: Oct 7 '05

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
Branimir Maksimovic
Guest
 
Posts: n/a
#4: Oct 11 '05

re: Pointer to class member array ... element? Possible?



"Kaz Kylheku" <kkylheku@gmail.com> wrote in message
news:1128707244.402046.158320@g49g2000cwa.googlegr oups.com...[color=blue]
>
> 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]

Try for example:
struct T{
char a[16];
};
int main()
{
char (T::*p)[16] = &T::a;
size_t p1 = *(size_t*)&p;
if(p) cout << sizeof(p)<<' '<<p1<<'\n';
}

p should return true but p1 could display 0.
If you write p = 0 explicitely then "if" wouldn't print.
It's more than a size_t.

Greetings, Bane.


Closed Thread