I'm trying to access and modify the private data member of a class by offset.
AFAIK, first thing is to calculate the offset, then access the member through the offset.
Here is my code.
class Test {
public:
int a;
int b;
private:
int c;
};
Test test;
cout << *(&test + &Test::b + 1); // access c
I got an error : "+" operator invalid, the right operand contains "int Test::*
" type.
There is a similar post, it printf
the offset of ab
as 1,4
, but when try like this:
cout << &Test::a << '\t' << &Test::b;
I got 1 1
.
My question:
1. Why cout
got the wrong result?
2. What does Test::*p
point to?
3. How to access and modify the Test::* pointer?(Or how to access the private member when doesn't know the offset?)