Pointers to class member functions are not Pointers

  • 2020-04-02 01:28:23
  • OfStack

1. Unlike regular Pointers, a pointer to a member does not point to a specific memory location, but to a specific member of a class, not to a specific member of an object. Often the clearest way to do this is to look at a pointer to a data member as an offset.

This offset tells you how many bytes the location of a particular member is from the starting point of the object.

2. Given the offset of a member within the class, we need the address of an object of that class in order to access the data member at that offset. This is where.* and - are needed > Operation of *. PC - > *pimC, request to add the address inside the pC to the offset inside the pimC in order to access the appropriate data member in the C object that the pC points to. Ac. *pimC, which requests the address of aC plus the deviation in pimC, is also to access the appropriate data member in the C object pointed to by the pC.
Ps:
* the member pointer dereference operator (.*) retrieves a member from an object or reference
* the member pointer arrow operator (- > *) gets a member through a pointer to an object

When you get the address of a non-static member function, you get not an address, but a pointer to the member function.

4. In order to dereferenced a pointer to a member function, you need an object or a pointer to an object. In the case of a pointer to a data member, the address of the object and the offset of the member need to be added in order to access the member. In the case of Pointers to member functions, you need to use the object's address as the value of this pointer, make function calls, and for other purposes.

An implementation of a pointer to a member function must itself store information such as whether the member function it points to is virtual or not, where to find the appropriate virtual function table pointer, and so on.

6. One more thing about Pointers to inline functions. It is legal for a function pointer to point to an inline function. However, calling inline functions through function Pointers will not result in inline function calls because the compiler cannot determine exactly what functions will be called at compile time. So at the call point, the compiler has no choice but to generate indirect, non-inline function call code.


Related articles: