-
September 7th, 2008, 09:10 AM
#16
Re: private members not so private.
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.
- Alon
-
September 8th, 2008, 03:05 AM
#17
Re: private members not so private.
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.
Last edited by _uj; September 8th, 2008 at 01:14 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|