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.
You can use pointers to member functions in the same manner as pointers to functionspointers to functionsA function pointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function. As opposed to referencing a data value, a function pointer points to executable code within memory.https://en.wikipedia.org › wiki › Function_pointerFunction pointer - Wikipedia. You can compare pointers to member functions, assign values to them, and use them to call member functions.
To access a member function by pointer, we have to declare a pointer to the object and initialize it (by creating the memory at runtime, yes! We can use new keyboard for this). The second step, use arrow operator -> to access the member function using the pointer to the object.
Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.
The this pointer is a pointer accessible only within the nonstatic member functions of a class , struct , or union type. It points to the object for which the member function is called. Static member functions don't have a this pointer.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With