Ponteiro de membro para o elemento da matriz
É possível definir um ponteiro para um membro e usá-lo posteriormente:
struct foo { int a; int b[2]; };<p></p>
int main() {<br> foo bar; int foo::* aptr=&foo::a; bar.a=1; std::cout << bar.*aptr << std::endl; }
Now I need to have a pointer to a specific element of an array, so normally I'd write
int foo::* bptr=&(foo::b[0]);
However, the compiler just complains about an"invalid use of non-static data member 'foo::b'
" Is it possible to do this at all (or at least without unions)?Edit: I need a pointer to a specific element of an array, so
int foo::* ptr
points to the second element of the array (foo::b[1]
).Yet another edit: I need to access the element in the array by
bar.*ptr=2
, as the pointer gets used somewhere else, so it can't be called withbar.*ptr[1]=2
or*ptr=2
.