+ 1

Can we access a private variable using pointer in C++

Suppose there is a code snippet class test { int a; }; int main() { test obj; int* ptr; } how can I store the address of obj .a in ptr. ?

5th Feb 2017, 6:45 AM
Suparno Karmakar
Suparno Karmakar - avatar
3 Answers
+ 1
You can use a function in test to point ptr at a, but if you do that you could also just make a public in the first place...
5th Feb 2017, 6:57 AM
Robobrine
Robobrine - avatar
+ 1
just using the pointer and c++ statements only.... no functions
5th Feb 2017, 8:19 AM
Suparno Karmakar
Suparno Karmakar - avatar
0
Well, you need to find the variable somehow, if you don't want functions you'll need a public pointer to a in test.
5th Feb 2017, 8:53 AM
Robobrine
Robobrine - avatar