Saturday, 7 July 2018

access private data members of a class without using a member or a friend function

#include
using namespace std;
 
class Test
{
private:
    int data;
public:
    Test() { data = 0; }
    int getData() { return data; }
};
 
int main()
{
    Test t;
    int* ptr = (int*)&t;
    *ptr = 10;
    cout << t.getData();
    return 0;
}


Out : 10


Note that the above way of accessing private data members is not at all a recommended way of accessing members and should never be used.

No comments:

Post a Comment