Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ class problem

Started by Grahf750 Jul 26, 2005 at 6:59 PM 3 replies 1k views
Original Post
Grahf750
Grahf750
I have a lifeform class that is a base class for the player and monster classes. I have a battle class in which I store the player and monsters who are in the battle. I want to make an attack function in the lifeform class, but it needs to know about the player and monsters in the battle. I cannot pass them as parameters though because it doesn't know what Monster and Player are. I can't include monster.h and player.h in the parent class. The only option I can think of is make the attack function in the battle class, unless there is something I am not thinking of which could very well be so.
SiCrane
SiCrane
If you have the function take pointers or references to the Monster and Player classes, you can use forward declarations. Ex:
class Player;class Monster;class LifeForm {  public:    void Fight(Player &, Monster &);};


On the other hand, I don't understand why you need to have the attack function in the life form class take both a player and a monster. Wouldn't it just need a reference to the life form it's attacking?

edit: More information on forward declarations and other good stuff.
Stephen R
Stephen R
What you can do is this:

//base.hclass CPlayer;class CMonster;class CBase {  void MyFunc(CPlayer* Player);  void MyFunc(CMonster* Monster);};//base.cpp#include "base.h"#include "player.h"#include "monster.h"

That way the base class can know about the derived classes. There is probably a better way of designing the system than that but I can't say what from the small amount of info you presented, but the code above will work.
BriTeg
BriTeg
Quote:
but it needs to know about the player and monsters in the battle.


Why does it need to know? Something doesn't sound right.

Brianmiserere nostri <font color=#BC
Grahf750
Grahf750
I thought something didn't seem right also that is why I am posted here. My battle function stores a vector of monsters and a player for that battle. In the attack function the attacking lifeform searches for other lifeforms around it that are against it player vs monsters, monster vs player. The target is chosen and the lifeform attacks. I then get information about the player or monster such as hit chance, dodge chance and then if the lifeform hits I call a take Dmg function on whatever is recieving the dmg.

So in searching for targets, and getting information about the target I need to know about them.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.