I have:
class Foo {
int a;
int b;
std::string s;
char d;
};
Now, I want to know the offset of a, b, s, d given a Foo*
I.e. suppose I have:
Foo *foo = new Foo();
(char*) foo->b == (char*) foo + ?? ; // what expression should I put in ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t know exactly why you want the offset of a member to your
struct, but an offset is something that allows to to get a pointer to a member given the address of a struct. (Note that the standardoffsetofmacro only works with POD-structs (which yours is not) so is not a suitable answer here.)If this is what you want to do, I would suggest using a pointer-to-member as this is a more portable technique. e.g.
Note that this will only work if
bisn’t private inFoo, or alternative you could form the pointer to member in a member function or friend ofFoo.