Is there any way I can access Private member variable of a class?
Editing:
Not from a member function or friend function but through an instance.
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.
Just cast it around, shift memory and cast back. (didn’t compile the code, but you should get the idea).
Since this is an interview question, I won’t go into why you shouldn’t do that. 🙂
EDIT:
Classes with virtual functions will have virtual table pointer somewhere there as well. I’m not sure if & will give you address of vt or address of first data member.
Alignment is 4 by default (right?), so if member you are reading does not align, shift by 2 bytes to get to the next one.