Re: private members not so private.
Quote:
Originally Posted by Peter_APIIT
What is conclusion about this topic ? Why this will happen ?
The conclusion is that C++ will not keep you from shooting yourself in the foot if you are hell-bent on doing so. Compiler rules exist to keep sane programmers from making mistakes, not to keep insane programmers from deliberately breaking them. I think Graham hit the nail on the head with this earlier.
It is worth mentioning that unions may not contain non-POD types, so this "method" of accessing private data doesn't apply to the vast majority of classes. You can still use pointers to access the data, but that's not the point. The point is don't do it.
Re: private members not so private.
Quote:
Originally Posted by Peter_APIIT
What is conclusion about this topic ? Why this will happen ?
The conclusion is that the OP hasn't accessed a private class member at all. He has modified the internal (compiler dependent) representation of an object in memory.
In C++ this is possible because you have language access to memory. A pointer is a memory address. It's just to start reading and writing to your hearts desire. If you want to do the same in Java you could serialize the object to file, change it there, and then read it back.
As I expressed it in my previous post; The OP hasn't bypassed class privacy, he has raped an object.