pointer-to-(data)members are illustrated here(from this answer: https://stackoverflow.com/a/670744/4416169 )
#include <iostream>
using namespace std;
class Car
{
public:
void carFunc(){}//added this myself
int speed;
};
int main()
{
int Car::*pSpeed = &Car::speed;
Car c1;
c1.speed = 1; // direct access
cout << "speed is " << c1.speed << endl;
c1.*pSpeed = 2; // access via pointer to member
cout << "speed is " << c1.speed << endl;
return 0;
}
Would it be useful, and atleast partially accurate, for the sake of memorizing the behavior of these pointers-to-data-members, to think about them as storing the memory offset to the pointed data member? This memory offset would then be added to the memory location of the specific class instance that is used to access the pointer-to-data-member in that specific instance.
If this mnemonic is flawed, what other better mnemonic is there for pointer-to-data-members?
Then there is the case of pointer-to-member-functions:
void (Car::*fptr)() = &Car::carFunc;
Would it be correct to imagine these pointer-to-member-functions as simple and clean function pointers pointing to a specified member function taking and returning whatever is specified BUT in addition and implicitly also taking a this
pointer from the instance calling them?