Thread: Getting address of private members?

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Farther than you think
    Posts
    11

    Getting address of private members?

    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

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    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

  4. #4
    Registered User
    Join Date
    Mar 2006
    Location
    Farther than you think
    Posts
    11
    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

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    private:
    	int a;
    public:
    	int* getAddressOfA() { return &a; }
    Better design, as aforementioned, would be to use accessor methods to work with 'a' with member functions like getA(), setA(int).

  6. #6
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    There's something fundamentally wrong with wanting to know the address of a private member. That's like... de-privatizing a private member.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by BMJ
    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!

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# and SQL
    By siten0308 in forum C# Programming
    Replies: 2
    Last Post: 07-09-2008, 12:34 PM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  4. Private Static class members
    By earth_angel in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2005, 06:37 AM
  5. webBrowser problem
    By algi in forum C# Programming
    Replies: 7
    Last Post: 08-19-2005, 09:35 AM