Computer Science Canada

class function access other private data member?

Author:  michaelp [ Sat Jan 05, 2008 9:35 pm ]
Post subject:  class function access other private data member?

Well, I am making a small game to test my C++ and get better at it too. ;D

What I want to know is: How can one class's function access another class's private data members?
Here is an example: (Not the code in my actual game, ignore any errors in the code)

code:
class Person
{
public:
   void function(Enemy enemy1)
   {
      cout << "you attacked, the enemy now has " <<
      //I want this function to access be able to access and change "dataMember" in the enemy class
   }
};

class Enemy
{
private:
   int dataMember;
};

Author:  DIIST [ Sat Jan 05, 2008 9:44 pm ]
Post subject:  Re: class function access other private data member?

michaelp @ January 5th 2008 wrote:
Well, I am making a small game to my C++ and get better at it too. ;D

What I want to know is: How can one class's function access another class's private data members?
Here is an example: (Not the code in my actual game, ignore any errors in the code)


The whole point of private members is hide methods,&variables from anything outside the class. If you want to access the members just make them public or protected( can only be accessed withen the class and subclasses). Wink

Author:  md [ Sat Jan 05, 2008 11:09 pm ]
Post subject:  RE:class function access other private data member?

I am not completely certain as I have never tried (or read about it, shame on me...) but private members are always inaccessible. You could use protected member data though. Then you just need to make Person a friend class to Enemy.

However! An enemy is a person, so maybe you are looking at this in the wrong way and Enemy should be derived from Person?

Author:  wtd [ Sat Jan 05, 2008 11:27 pm ]
Post subject:  RE:class function access other private data member?

In all likelihood md is correct with regards to Enemy being derived from Person. Of course, the assumes that an Enemy can only be a Person. Perhaps Enemy should be a purely virtual base class, and EnemyPerson should derive from both Person and Enemy.

Author:  michaelp [ Sun Jan 06, 2008 8:43 am ]
Post subject:  RE:class function access other private data member?

I'll try using protected data members first...

How do you make a class a friend of another class?

Author:  md [ Sun Jan 06, 2008 11:15 am ]
Post subject:  RE:class function access other private data member?

c++:
class foo { ... };

class bar
{
friend class foo;
...
}

Author:  OneOffDriveByPoster [ Sun Jan 06, 2008 11:48 am ]
Post subject:  Re: RE:class function access other private data member?

md @ Sat Jan 05, 2008 11:09 pm wrote:
I am not completely certain as I have never tried (or read about it, shame on me...) but private members are always inaccessible. You could use protected member data though. Then you just need to make Person a friend class to Enemy.
Afaik, a friend of a class can access private members of the class granting friendship.

Author:  michaelp [ Sun Jan 06, 2008 11:50 am ]
Post subject:  RE:class function access other private data member?

Okay, thanks, the friend class and protected data members worked. Very Happy

Author:  CodeMonkey2000 [ Sun Jan 06, 2008 2:47 pm ]
Post subject:  RE:class function access other private data member?

Shouldn't enemy inherit person?

Author:  md [ Sun Jan 06, 2008 3:31 pm ]
Post subject:  Re: RE:class function access other private data member?

CodeMonkey2000 @ 2008-01-06, 2:47 pm wrote:
Shouldn't enemy inherit person?

It could, or as wtd pointed out on irc person could inherit enemy as not all enemies are persons.

I'm thinking I wouldn't be using such classes in anything I wrote; and definitely not friend functions accessing private data. Better class hierarchies make inheritance easy, and using friend functions/classes to access private data is almost always bad practice.

Author:  wtd [ Sun Jan 06, 2008 11:16 pm ]
Post subject:  RE:class function access other private data member?

No no... I'm suggesting using both interface and implementation inheritance.


: