Jump to content

class/class pointer question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
MerakSpielman

MerakSpielman

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
I'm practicing creating classes that themselves contain pointers to other classes. I am also practicing creating private class variables that can only be accessed via public member functions. Since I think in terms of video games, those are the sorts of classes I'm making.

for example:


class CRace

{

public:

	CRace(){};

	~CRace(){};

	void SetBonStr(int v){Bonus_Strength = v;}

	void SetBonInt(int w){Bonus_Intelligence = w;}

	void SetBonAgl(int x){Bonus_Agility = x;}

	void SetBonCon(int y){Bonus_Constitution = y;}

	void SetRaceName(string u){name = u;}

	int GetBonStr(){return Bonus_Strength;}

	int GetBonInt(){return Bonus_Intelligence;}

	int GetBonAgl(){return Bonus_Agility;}

	int GetBonCon(){return Bonus_Constitution;}

	string RaceName(){return name;}

	


private:

	int Bonus_Strength;

	int Bonus_Intelligence;

	int Bonus_Agility;

	int Bonus_Constitution;

	string name;

}Human,Dwarf,Elf,Gnome,RatMan,SnakeMan;
Now perhaps I'm overdoing it a bit, but this IS for practice. You can see that the real variables here are all private, and are changed/accessed only by public member functions. Simple enough. You can also see that I'm pre-naming 6 races to exist in my game - Human, Dwarf, Elf, Gnome, RatMan, and SnakeMan. I define all of these races' variables elsewhere, and it's not worth re-pasting that code here. Just remember that they all HAVE those variables - they're not left empty.

Now I'm trying to create a class for the Player. This class contains a pointer to one of the existing races, like so:


class CPlayer : public CCreature

{

public:

	CPlayer(){};

	~CPlayer(){};

	void SetClass(int y){Class = y;}

	CRace *Player_Race;


private:

	int Class;

};
CCreature is a higher-level class which all creatures, player or not, have in common. It contains the stats, such as Strength. I didn't paste it because it's not important for the actual question I'm getting at.

See the line
CRace *Player_Race;
I can assign this easily enough by putting a line like this in the code:
Player.Player_Race = &Dwarf;
and then verify that it worked by testing with a line like this:
cout << endl << Player.Player_Race->RaceName;
I can access all the member functions of the players race just like I should be able to. It works perfectly.

Except... my goal is to have all the variables for my classes be private, and accessed only by member functions. Right now my class pointer is public. Not what I want!

So I tried changing my player class to this:

class CPlayer : public CCreature

{

public:

	CPlayer(){};

	~CPlayer(){};

	void SetClass(int y){Class = y;}

	void SetRace(CRace *x){Player_Race=x;}


private:

	int Class;

	CRace *Player_Race;

};
Note that now I'm using the member function SetRace, which takes a CRace address and assigns it to the private *Player_Race pointer.
I can assign a race to the player now by using this:

Player.SetRace(&Dwarf);
Again, easy enough.

I hope you've been following. Now here comes my actual question. :D

I'm trying to access the member functions of my player's race. Before I changed it I could use Player.Player_Race->function. But now that my Player_Race is private... I'm not sure how to get at the member functions of the race.

Is this an exception to the general rule of "keep class variables private if possible?" It's so easy to access the Race member functions when keep the Player_Race public... maybe I should just do it that way and not worry about it.

But I AM learning, and I was a bit annoyed that until this problem I had figured everything out and gotten it working.

I thought of maybe using a public pointer to the private pointer... but I couldn't figure out how to declare it, and at that point it starts to kind of seem silly. If I'm just going to have a public pointer pointing right at it, why keep it private?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You may want to think about a slightly different rule:
Keep member variables private if they require data validation.

Another approach is to have an accessor function that returns the address of the member. The real question in my mind, though, is why are you using pointers in this situation?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
MerakSpielman

MerakSpielman

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
I'm using pointers because the races are 6 pre-established CRace objects. The player could conceivably choose any of them, so I have a pointer ready to point at the one he picks.