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

a simple design question..?

Started by MTclip Jul 31, 2005 at 5:03 PM 5 replies 550+ views
Original Post
MTclip
MTclip
okay say I have ITEM as the base class WEAPON || SHIELD || POTION will be derived from ITEM ITEM will have actions.. multiple actions per item will be possible what would be a better solution..

//(A)
class CITEM
{
..
..
virtual action1();
virtual action2();
virtual action3();
..
..
virual action9();
}

// - OR - //
//(B)
Class CITEM
{
..
..
virtual actions();
}

a) there would be a limited number of actions perItem.. b) the derived classes would use if statements in the action to determine what action should take place (c)-or - could i just have "virtual actions()" and in the derived overload it for the number of actions i need? - is that allowed in c++ anyways... hope i make sense thanks
Wavarian
Wavarian
How about:

enum ItemActionId{	IA_ACTION_A,	IA_ACTION_B};class Item{public:	Item()	{	}	virtual ~Item()	{	}	virtual void PerformAction(ItemActionId actionId)	{		// Default handler		switch(actionId)		{		case IA_ACTION_A:						// ...			break;		case IA_ACTION_B:			// ...			break;		}	}};class Weapon : public Item{public:	Weapon()	{	}	~Weapon()	{	}	void PerformAction(ItemActionId actionId)	{		// Only handles IA_ACTION_B		if(actionId == IA_ACTION_B)		{			// Perform action.		}	}};

Easier to add extra functionality later on. It's probably better to do it this way too, since if you have a Potion, you only ever want to see methods that are directly related to what that Potion can do.
Drakkcon
Drakkcon
If you want a less efficient but cooler way of doing it, you could create an abstract base class cBaseAction. Inherit from cBaseAction and overload its Execute() function to make it do something, and in its constructor, change its _name and _type etc.

This is the approach I'm taking with my scripting engine, it's a lot slower than a simple switch statement though....

[Edited by - Drakkcon on August 1, 2005 2:18:32 PM]
MTclip
MTclip
heh.. thanks for that article.. it was interesting....
I was using ENUMS and static_casts but now i think i
am gonna go with virtual functions..


i have another question..

say you have this..

class base{ virtual void doDtuff(void);}class derived : public base{ void doStuff(void); void doStuff(int frame); int  doStuff(void);}std::vector<base*> vbase;derived myDerived;myDerived = new derived();vbase.push_back(myDerived);vbase.doStuff();   vbase.doStuff(5);int count = vbase.doStuff();



will it be able to call the overloaded function..
or do i have to create a virtual for each overload at the base level?

...?


Argus2
Argus2
Hmm, I think a method's signature is made up of the name and arguments. doStuff(int) won't override doStuff(void), and I don't think you can have doStuff(void) twice, even if it returns different things. Should be easy enough to test though.
Deyja
Deyja
The article missed one great solution. Messages. Wavarian outlined the best solution of those presented so far.

Topic Locked

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

Sign in to reply to this topic.