| 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? |