ptr to member

T

trying_to_learn

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 = arg+i;
}
}
};
 
A

Alf P. Steinbach

* trying_to_learn:
im trying to index through an array that is member of a class using ptr
to member

Instead provide a []-operator.

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

Don't use it.

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

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

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 = arg+i;
}
}
};


Like this:

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

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

But don't use it.
 
R

Rolf Magnus

trying_to_learn said:
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

Uh... a compiler suggesting a reinterpret_cast and a C style cast... evil.

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?
how do i define a ptr to member arr?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top