I do not understand the two lines mentioned below in the code that I have provided here. Why I need to use int*
? How I am accessing the private variable? I am not sure what I am doing with these two lines. Please explain in detail.
Problematic lines:
int *p = (int *) &s;
*p=10;
Main Code:
#include <iostream>
using namespace std;
class sample {
private:
int a;
public:
void function () {
a=5;
}
void printA () {
cout<<"value is "<<a<<endl;
}
};
int main () {
sample s;
s.function();
s.printA();
int *p = (int *) &s;
*p=10;
s.printA();
}