I wrote the following program to access an object's private members via its address. How is it possible that the following code did not give me ANY error? The code compiles
and gives me the right answer. Does this mean C++ is inferior to other languages in its ability to truly implement data-hiding?? Thanks!
#include<iostream>
class example{
private:
int variable;
public:
void setprivate(void);
};
class example2{
public:
example *xyz;
};
void example::setprivate(void){
variable = 55;
}
int main()
{
example e;
e.getprivate();
example2 e2;
e2.xyz = &e;
int *ptr = (int *)e2.xyz;
std::cout<<*ptr<<std::endl;
return 0;
}