Thread: private class member access

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    27

    private class member access

    I noticed this when I was doing some programming in my game.
    Code:
    struct coord
    {
    short int x;
    short int y;
    coord* cLP;//this is a linked list pointer
    Character* cReside;//this points to a character object to indicate
                                    //that a character is in this location
    }
    That is the gist of the coord struct. Now, when I was coding the targeting system, I inadvertently used the cReside pointer to access a private data member of the Character class, but much to my surprise, the program compiled!
    Code:
    cReside->pItem[1].Unequip
    Now granted, the pointers of the coord struct are public, but the pItem array is still private in the Character class. Can somebody tell me why this access is possible?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Could you post a compilable example?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    27
    Code:
    class Character
        {
        public:
            TryThis(Character*);
        private:
            short int Atk;
            short int Def;
            //other data here, yadda yadda
        };
    
    void Character::TryThis(Character* arg1)
        {
        arg1->Atk=1234;
        }
    
    int main()
        {
        Character DummyNumber1;
        Character DummyNumber2;
    
        DummyNumber1.TryThis(&DummyNumber2);
        return 0;
        }

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    This is perfectly legal. You acces a private member of of an object from within a memberfunction of that objects class. And yes it doesn't matter that it is a member of another instance of that class.
    Kurt

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    27
    I wanted to make sure becuase as long as I have been programming this is the first time I have run into that situation. Considering the whole concept of "data hiding", I was surprised to find that it was possible to do that, as I had never tried to do so deliberately. If I needed to access private data from a different instance of a class, I would just use the normal interface functions that would have to be used when executing externally to the class.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But some member functions really need privileged access to different instances of the same class. Think of copy constructors, for example - it needs to copy internal data out of the source object. It can do that either by accessing them directly or by using public accessors - but that would require the class to expose accessors for things that should remain hidden.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by DarkMasterBosel View Post
    I wanted to make sure becuase as long as I have been programming this is the first time I have run into that situation. Considering the whole concept of "data hiding", I was surprised to find that it was possible to do that, as I had never tried to do so deliberately. If I needed to access private data from a different instance of a class, I would just use the normal interface functions that would have to be used when executing externally to the class.
    One, it makes sense from a logical standpoint to allow one object to modify another object of the same class. There should be no danger of messing up internal state, because the whole point of the class is to understand how to maintain its own internal state. If one object is messing up another object of the same class, it means your code is buggy, not that you're defeating encapsulation.

    Two, even if the compiler tried to prevent you from changing another object, it would be pointless, since you could make it happen anyway by writing a member function which did the same manipulation and invoking it on the other object. For instance, suppose the compiler did NOT let you do this:

    Code:
    class a
    {
        void frob(a &other) { other.x = 123; }
        int x;
    };
    Well hell, you'd just do this:

    Code:
    class a
    {
        void frob(a &other) { other.frobYou(); }
        void frobYou() { x = 123; }
        int x;
    };
    Right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Private class member not being set
    By cboard_member in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2007, 11:38 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Can't Access the private member from base class
    By planet_abhi in forum C# Programming
    Replies: 3
    Last Post: 01-09-2006, 04:30 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. Private Data Access
    By suetya in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2002, 01:53 PM