Connecting Tech Pros Worldwide Help | Site Map

ptr to member

trying_to_learn
Guest
 
Posts: n/a
#1: Jul 22 '05
im trying to index through an array that is member of a class using ptr
to member

in main i tried making a ptr to the member 'arr' :

const int * testclass7::* ptrToMemberIntPtr = &testclass7::arr;
------------------------------------------------------
the compiler cries:
cannot convert from 'int (testclass7::*)[10]' to 'const int *testclass7::* '
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
------------------------------------------------------
I cant figure this out... isnt arr basically a const int * ?
how do i define a ptr to member arr?

class testclass7
{
public:
int arr[10];
testclass7(int arg=1)
{
for (int i=0;i<sizeof(arr) / sizeof(*arr);i++)
{
arr[i] = arg+i;
}
}
};
Alf P. Steinbach
Guest
 
Posts: n/a
#2: Jul 22 '05

re: ptr to member


* trying_to_learn:[color=blue]
> im trying to index through an array that is member of a class using ptr
> to member[/color]

Instead provide a []-operator.

Pointer to member is very seldom necessary, and pointer to data member
is absolutely EVIL.

Don't use it.

[color=blue]
> in main i tried making a ptr to the member 'arr' :
>
> const int * testclass7::* ptrToMemberIntPtr = &testclass7::arr;
> ------------------------------------------------------
> the compiler cries:
> cannot convert from 'int (testclass7::*)[10]' to 'const int *testclass7::* '
> Types pointed to are unrelated; conversion requires reinterpret_cast,
> C-style cast or function-style cast
> ------------------------------------------------------
> I cant figure this out... isnt arr basically a const int * ?[/color]

No, it's not const, and it's not a pointer: it's an array.

[color=blue]
> how do i define a ptr to member arr?
>
> class testclass7
> {
> public:
> int arr[10];
> testclass7(int arg=1)
> {
> for (int i=0;i<sizeof(arr) / sizeof(*arr);i++)
> {
> arr[i] = arg+i;
> }
> }
> };[/color]

Like this:

int main()
{
testclass7 obj;
int (testclass7::* p)[10] = &testclass7::arr;

for( int i = 0; i < 10; ++i )
{
std::cout << (obj.*p)[i] << std::endl;
}
}

But don't use it.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Rolf Magnus
Guest
 
Posts: n/a
#3: Jul 22 '05

re: ptr to member


trying_to_learn wrote:
[color=blue]
> im trying to index through an array that is member of a class using ptr
> to member
>
> in main i tried making a ptr to the member 'arr' :
>
> const int * testclass7::* ptrToMemberIntPtr = &testclass7::arr;
> ------------------------------------------------------
> the compiler cries:
> cannot convert from 'int (testclass7::*)[10]' to 'const int *testclass7::*
> ' Types pointed to are unrelated; conversion requires reinterpret_cast,
> C-style cast or function-style cast[/color]

Uh... a compiler suggesting a reinterpret_cast and a C style cast... evil.
[color=blue]
> ------------------------------------------------------
> I cant figure this out... isnt arr basically a const int * ?[/color]

No. arr is an array of 10 ints. Look at the error message. What you'd need
is a member pointer to an array of int, not a member pointer to a pointer.
Anyway, why do you think you need a member pointer at all?
[color=blue]
> how do i define a ptr to member arr?[/color]

Uhm, look at the error message. It tells you.
[color=blue]
> class testclass7
> {
> public:
> int arr[10];
> testclass7(int arg=1)
> {
> for (int i=0;i<sizeof(arr) / sizeof(*arr);i++)
> {
> arr[i] = arg+i;
> }
> }
> };[/color]

Closed Thread