Okay...
How the heck do I get the address of a private class member? Sure, I can get it if it's not private, but...can anyone give me a hand? Thanks.
-Aramil
Okay...
How the heck do I get the address of a private class member? Sure, I can get it if it's not private, but...can anyone give me a hand? Thanks.
-Aramil
Why do you want it? Are you allowed to modify the class? If not, then you shouldn't need the address of a private variable. If you are allowed to change the class, and you really do need access to the address of the private member (probably bad design), then add a method that returns it.
It does defeat the purpose of a private function if you can access it outside a class. You can use accessor and modifier methods to access them from the outside though
Amish
Hmmm...okay, guess it would be bad design. I'm pretty sure I can come up with an alternative. Thanks.
(Funny...my CS teacher says I'm always doing things the hard way...calls me "Mr. Fancy Pants")
-Aramil
Better design, as aforementioned, would be to use accessor methods to work with 'a' with member functions like getA(), setA(int).Code:private: int a; public: int* getAddressOfA() { return &a; }
There's something fundamentally wrong with wanting to know the address of a private member. That's like... de-privatizing a private member.
I couldn't agree more!Originally Posted by BMJ
Like everyone, said, it defeats the purpose of making it private in the first place, just use an accessor instead. But, if you *really* want to know whether it's possible or not, and, if so, how? here's an ugly way (which I do not advocate or condone, just to set the record straight):
Assign the address of the instance to void* and typecast it to the desired type, and you can work with a private member