I am trying to know how pointers to members work in C++.
I looked at some places, and found that they store some sort of offset. And use the fact that members occur in memory in the same order in which they are declared in the class/structure definition.But...
#include <iostream>
#include<typeinfo>
using namespace std;
struct S
{
S(int n): mi(n) {}
int mi;
int k;
int f() {return mi+k;}
};
int main()
{
S s(7);
int S::*pmi = &S::mi;
int S::*lop = &S::k;
int (S::*sf)() = &S::f;
cout<<&S::mi<<endl<<&S::k<<endl<<&S::f<<endl<<pmi<<endl<<lop<<endl<<sf<<endl;
cout<<typeid(lop).name()<<endl;
}
I expected to see some sort of offsets. But, all values in the first line with cout<< , gave 1 as output.
I don't understand , if all are giving 1, where is the information regarding offsets being stored?
It'd be really helpful if you can explain what is really going on.