• Advertisement
Sign in to follow this  

a simple design question..?

This topic is 4583 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

...?


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
The article missed one great solution. Messages. Wavarian outlined the best solution of those presented so far.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement