Re: Re: Fun with C++
By: dx2 to Nightfox on Wed Nov 27 2013 00:20:00
Redefining private to public is such a cheap trick.... A real programmer will simply take the address of the object and offset into it. Encapsulation is nothing but a compiler contrivance designed to force people into using the proper interfaces.
char* dataPtr = (char*) &dataObj;
unit privVal;
memcpy(&privVal, dataPtr + 4, 2);
I think redefining private to public could be useful for testing purposes though, in situations where you can't change the code but want to verify that certain data members are what they should be. I think that's a simpler solution than taking the address of the object and getting the offset to the data member (which seems even more hacky). In order to do that, you'd need to know the order in which the data members are declared, and sometimes that could be difficult to determine. Also, I'm not sure that all compilers will create the same object code, so I'd wonder if that solution would work with all compilers.
Nightfox
---
þ Synchronet þ Digital Distortion BBS - digitaldistortionbbs.com