Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 639
Darlyn Avatar asked Nov 07 '16 22:11

Darlyn


People also ask

Can we create a pointer to a member function?

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.

How do you access member functions using pointers?

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.

How do you pass a pointer to a function?

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.

What is the type of this pointer in its member 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.


1 Answers

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

like image 98
Earth Engine Avatar answered Oct 05 '22 13:10

Earth Engine