How do I polymorph this?

Dec 27, 2009 at 4:42am
Please tell me how to polymorph this. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Spell
{
protected:
   //some variables here
};

class Spell_A : public Spell
{
protected:
   //some variables here
public:
   //this spell grows a tree at the given position
   void activate_spell(int position_x, int position_y);
};

class Spell_B : public Spell
{
protected:
   //some variables here
public:
   //this spell damages a person
   void activate_spell(Person* pointer);
};

class Spell_C : public Spell
{
protected:
   //some variables here
public:
   //this spell turns a rock into a golem
   void activate_spell(Rock* pointer);
};
Dec 27, 2009 at 5:24am
Spell should have an activate_spell() function declared something like
virtual void activate_spell(/*parameters*/)=0;
In order for this to work, all derived classes have to implement activate_spell(). Oh, and all versions of the function have to take the exact same parameters and return the same type.

By the way, "to polymorph" isn't a word. The closest expression would be "to make X polymorphic".
Last edited on Dec 27, 2009 at 5:26am
Dec 28, 2009 at 1:58am
Well, that's what I meant to ask, different classes take different parameter for activate_spell so how do I make it polymorphic?
Dec 28, 2009 at 2:07am
Polymorphism requires that all derived classes have a shared interface. This is so functions from derived classes can be called using pointers to the base class without run time type information.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Base{
public:
	virtual ~Base();
	virtual void foobar(int)=0;
};

class DerivedA:Base{
public:
	~DerivedA();
	void foobar(int);
};

class DerivedB:Base{
public:
	~DerivedB();
	void foobar(int);
};

int main(){
	std::vector<Base *> v;
	v.push_back(new DerivedA);
	v.push_back(new DerivedB);
	v.push_back(new DerivedA);
	v.push_back(new DerivedA);
	for (int a=0;a<v.size();a++)
		v[a]->foobar(a); //No casting or type check needed
}

If this requirement can't be met, the class structure can't be made polymorphic. At least not AFAIK.

In your case, you could do it by having the base class function take all the parameters, then each of the derived classes will use the parameters as they need.
Last edited on Dec 28, 2009 at 2:17am
Dec 28, 2009 at 2:10am
That is true; AFAIK virtuals must all have the same parameter list.
Basically, you call the virtual through a pointer or reference to a base-class item.
That reference could point to a base class or it could point to an object of derived class. Both are legal because the pointer will only refer to the "base-class" subsection of the derived object. It is legal for this to remain undetermined at compile time. The compiler will then generate code that will differentiate which version of the virtual to call, based on the actual item to which you are referring/pointing, at runtime. This is what is known as polymorphic behaviour. (IF I am right and that is one big if.)
And here's something I've noticed that I must ask about: Why is it that so many examples refer to foo, bar or the predominant combination thereof, foobar?
So to do what you'd want to do, OP, you would have to:
A) Make all those activate_spells use the same set of arguments.
B) (At least this is what I would do) Define a PURE virtual called activate_spell with the desired argument list in the base class Spell. A pure virtual's declaration is followed by =0
virtual void foobar(int) = 0;
and cannot be called. It is created solely so that inheritees will (must, if they intend to use it at all) be able to redefine the function. However the function itself does not exist. In addition, this will turn Spell into an abstract class. Objects of an abstract class cannot be declared (although their derivations can, and those derivations will contain the appropriate subobjects).
C) Create a pointer or reference to a Spell (I believe this is still legal despite the abstractness of Spell, due to the fact that it could point to a concrete derivation, someone could check) and bind it to an object of a class derived from spell.
D) Referring to that reference as though it were the class object in question, call the virtual with the desired arguments.
And now, in step D, the compiler would call the virtual matching the actual (dynamic) type of the class object pointed to.
But it has been a while since I studied my polymorphic behaviors.
Last edited on Dec 28, 2009 at 2:15am
Dec 28, 2009 at 2:21am
Why is it that so many examples refer to foo, bar or the predominant combination thereof, foobar?
http://en.wikipedia.org/wiki/Metasyntactic_variable
http://www.catb.org/~esr/jargon/html/M/metasyntactic-variable.html
Dec 28, 2009 at 2:27am
We can find memes and references to popular culture even in programming...
Dec 28, 2009 at 4:39am
Why is it that so many examples refer to foo, bar or the predominant combination thereof, foobar?


I have one co-worker that uses foo and bar, and another that uses Fred. Both of them are 50+ years old. Not sure that has any significance.

I do try to make some reference to Wilma anytime Fred comes up in a conversation though.
Dec 28, 2009 at 12:41pm
Topic archived. No new replies allowed.