Tuesday, March 27, 2007

stupid hacks ? JAVA | C++ : Py

#include "iostream"

class a_class{
public:
a_class (){ i=1; };
virtual ~a_class (){};

int* steel_it (){ return &i; }; // this is allowed... (but a very bad idea)
void showit (){ std::cout << i << std::endl; };
private:
int i; // this should be save in here...
};

int main(){
a_class a; // make it
a.showit (); // see it
// a.i=3; // this is not allowed
int *i = a.steel_it (); // steel it... and this messes the privacy up...
*i=2; // and fake it...
a.showit (); // see it...
}

well, that's C/C++, not nice but clear to read and forced by the dereferencing of i in the steel_it() member function.

But who ever thought JAVA is better is wrong, it is not and you can do the same dirty things, just return an private array in the same way and you get an reference you can mess around with outside the class -- good-by protection. And worse, it is not as clear to envision what is going on as it is in C.

PyZahl's lesson leared today.

No comments:

Post a Comment