I'm developing a basic attack structure in a text-based game to experiment with class structures. I have three classes; a UI class, an enemy class, and a player class. I developed the code successfully without the UI class first but it seemed bulky and real messy to be honest.
The class below seems more scalable, and much cleaner. The problem I'm having is that the attackState() and defenseState() functions would need access to my two other classes of types cEnemy and cPlayer. Is there a way in which I can access public functions of cEnemy and cPlayer classes within the UI class?
Isn't the friend keyword used to access private members of the given classes? I'm only trying to access public functions of these classes before their instances are declared.
I figured there might be a way to use pointers within the class UI to access say cEnemy or cPlayer? I've tried variations on the code below, but I'm both not sure if this is even possible.. or if it uses correct pointer syntax in the first place.
1 2 3 4 5
//Take pointers to two classes, and do various things with them.
void attackState(cEnemy *Skel, cPlayer *Play)
{
Skel->reduceHP(Play->getAttackHP() );
}
That looks like correct syntax. Just pass in &Skeleton and &Player when you call attackState().
Are Skeleton and Player members of the UI class? If they are, you don't even need to have them as parameters to the attackState function - it can just use them directly.
The reduceHP() and getAttackHP() methods just need to be public in cSkeleton and cPlayer.
Oooohh.... This is one of those 'duh' moments I think lol. I totally spaced on making these into nested classes. Thanks Jim! Things should run a lot smoother now that I know the easiest way to do this.