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;

}

Recommended Answers

All 3 Replies

Does this mean C++ is inferior to other languages in its ability to truly implement data-hiding??

There's no such thing as a superior or inferior programming language; only the right tool for the job, given all the circumstances, and if you go around using words like that you'll annoy people.

The code compiles

No it doesn't. The code uses the non-existent function getprivate() and doesn't compile. Also, I don't know why you have that example2 class in there. it's doing nothing. You can instead simply point directly at the example object; int *ptr = (int *)&e;

If you'd used the existent function setprivate(), it would compile.

C++ trusts you. C++ lets you have control. If you make a pointer, and you insist that it points to an integer, then the compiler will trust you. If you then demand to know what the value of that integer is, the compiler will trust you and will dereference the pointer.

In this case, you made a pointer that pointed to an example object (&e), but you told the compiler that it's not an example object. You insisted that it's an int. You insisted. The compiler trusts you, because this is C++ and you're expected to know what you're doing. You can point anywhere you like in memory, and demand that it be read as an object of any type you like. It's generally a very bad idea and a sign of a bad design, but this is C++ and the programmer is trusted with power.

The layout of an example object is very simple, because it's a simple object. There's just a single int in it. So you got lucky, when you pointed at an example object and insisted that it was an int.

This is C++. In C++ (and C) you are allowed to point anywhere you like in the memory, and try to read it however you like. In this case, you pointed at an example object and insisted on reading it as an int.

It's nothing - absolutely nothing - to do with data hiding. This is you pointing at some memory and demanding that it be read as an int. In this case you got "lucky" (or maybe unlucky, depending on your view), because it just happens that the memory location you pointed at does have an int in it. C++ allows you to do this, because you're expected to know what you're doing. If you decide that this means C++ is a bad language, that's up to you; there are many languages you can try that do not trust you and will not let you reach into the memory. A great many programmers cannot be trusted with this kind of power.

commented: With great power comes great responsibility! +9

As Bjarne Stroustrup has said

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg.

IMHO data hiding has nothing to do with keeping some memory locations savely locked up in a locker. In a class a field(or whatever) is marked private, because it is meant to be used only by the class. In this way other classes can not accidently manipulate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.