trolkura trolkura - 1 month ago 14
C++ Question

Pointer to a member function

having structure

struct Person{
Person( int i , int g):id(i),age(g){};
int id;
int age;
};


i can dynamicly return member data by passing pointer to member data as argument to function. e.g

int extract( Person * p , int Person::* param)
{
return p ->*param;
}


and invoking using

Person *p = new Person (10 , 20 );
cout << extract(p , &Person::id )<< endl;


But my question is , why does this work? We are passing
&Person::id
basicly memory , but Person is an r-value which goes against definition.

I appreciate all explanation / clarification of my possible misunderstanding of topic.
Thanks.

Answer

Pointer to member in C++ can be understood as an "offset" definition. Although there are some complications when you have virtual functions etc, but in your case this is good enough.

So, basically your code is just like (if we turn data types to "raw" data)

int extract(void *p, int offset)
{
    return *((int*)(p+offset));
}

(technically speaking, the above code is not compile, because a void * pointer cannot be used in addition expression as the compiler don't know the size of the pointer, but let's ignore this for now.)

and when you call it, the code looks like

extract(p, __offsetof(Person, id));

where __offsetof is a pseudo operator that the compiler will calculate it at compile time.

You can see there is no magic here. What C++ helps you is that the offset is protected in a special data type, so you are not allowed to change its internal and so avoid breaking things apart.

Additional information about C/C++ pointer casting and arithmetic

The code above is quite basic to C/C++ users, although it is not the form a C++ professional would like to see (shall use static_cast or reinterpret_cast instead of C stype casting).

I see the question asker asks further explaining about C/C++ pointer arithmetic, and this is a bit off-topic as it have nothing to do with the question asking here, so instead I give some further reference about this topic.

Pointer Arithmetic

http://www.technoplaza.net/programming/lesson9p2.php

http://www.informit.com/articles/article.aspx?p=686170&seqNum=8