T
Tigera
Greetings,
I too have succumbed to the perhaps foolish urge to write a video
game, and I have been struggling with the implementation of multiple
dispatch. I read through "More Effective C++" by Scott Meyers, and
though I am impressed with his implementation, I wanted to find a way
to use multiple dispatch in a way that was less complicated.
Forgive me if the result, below, has been posted before or written of
before - I haven't read the "Purple Book" at all, so maybe it's all
been done before. What I'd like to know is what I'm missing - this
compiles on my machine, but am I doing something horribly wrong by
trying this? If I add a class later, like one out of a dynamically-
linked library, am I going to have to recompile everything? It just
seems to be too easy to be true.
//File: base_dispatch.h
#ifndef __BASEDISPATCH__
#define __BASEDISPATCH__
class BaseDispatcher
{
public:
virtual void Dispatch(void* A) = 0;
};
#endif
//File: dispatchany.cpp
#ifndef __DISPATCHANY__
#define __DISPATCHANY__
#include "base_dispatcher.h"
template<class T=BaseDispatcher, class U=BaseDispatcher>
class DispatchAny
{
public:
static void Dispatch(T* dispatcher, void* arg)
{
dispatcher->Dispatch(arg);
}
static void Dispatch(T* dispatcher, U* arg)
{
dispatcher->Dispatch(arg);
}
};
#endif
//File: testdispatchany.cpp
#include "dispatchany.cpp"
#include <iostream>
using std::cout;
using std::endl;
class Attack;
class Ability
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
ability"<<endl; }
virtual void Dispatch(Attack* a) { cout<<"Ability dispatches
attack"<<endl; }
};
class Attack
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
attack"<<endl; }
virtual void Dispatch(Ability* a) { cout<<"Attack dispatches
ability"<<endl; }
};
class Melee : public Attack
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
melee"<<endl; }
virtual void Dispatch(Ability* a) { cout<<"Melee dispatches
ability"<<endl; }
};
int main()
{
Ability* a = new Ability();
Attack* b = new Attack();
int* test = new int(5);
Melee* melee = new Melee();
DispatchAny<Ability, Attack> c;
DispatchAny<Attack,Ability> d;
DispatchAny<Melee,Ability> e;
c.Dispatch(a,b);
d.Dispatch(b,a);
e.Dispatch(melee,a);
d.Dispatch(melee,a);
c.Dispatch(a,a);
c.Dispatch(a,test);
delete a;
delete b;
delete melee;
delete test;
return 0;
}
I seek the wisdom of the opinions of those more experienced than I,
and I thank you for them.
I too have succumbed to the perhaps foolish urge to write a video
game, and I have been struggling with the implementation of multiple
dispatch. I read through "More Effective C++" by Scott Meyers, and
though I am impressed with his implementation, I wanted to find a way
to use multiple dispatch in a way that was less complicated.
Forgive me if the result, below, has been posted before or written of
before - I haven't read the "Purple Book" at all, so maybe it's all
been done before. What I'd like to know is what I'm missing - this
compiles on my machine, but am I doing something horribly wrong by
trying this? If I add a class later, like one out of a dynamically-
linked library, am I going to have to recompile everything? It just
seems to be too easy to be true.
//File: base_dispatch.h
#ifndef __BASEDISPATCH__
#define __BASEDISPATCH__
class BaseDispatcher
{
public:
virtual void Dispatch(void* A) = 0;
};
#endif
//File: dispatchany.cpp
#ifndef __DISPATCHANY__
#define __DISPATCHANY__
#include "base_dispatcher.h"
template<class T=BaseDispatcher, class U=BaseDispatcher>
class DispatchAny
{
public:
static void Dispatch(T* dispatcher, void* arg)
{
dispatcher->Dispatch(arg);
}
static void Dispatch(T* dispatcher, U* arg)
{
dispatcher->Dispatch(arg);
}
};
#endif
//File: testdispatchany.cpp
#include "dispatchany.cpp"
#include <iostream>
using std::cout;
using std::endl;
class Attack;
class Ability
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
ability"<<endl; }
virtual void Dispatch(Attack* a) { cout<<"Ability dispatches
attack"<<endl; }
};
class Attack
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
attack"<<endl; }
virtual void Dispatch(Ability* a) { cout<<"Attack dispatches
ability"<<endl; }
};
class Melee : public Attack
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
melee"<<endl; }
virtual void Dispatch(Ability* a) { cout<<"Melee dispatches
ability"<<endl; }
};
int main()
{
Ability* a = new Ability();
Attack* b = new Attack();
int* test = new int(5);
Melee* melee = new Melee();
DispatchAny<Ability, Attack> c;
DispatchAny<Attack,Ability> d;
DispatchAny<Melee,Ability> e;
c.Dispatch(a,b);
d.Dispatch(b,a);
e.Dispatch(melee,a);
d.Dispatch(melee,a);
c.Dispatch(a,a);
c.Dispatch(a,test);
delete a;
delete b;
delete melee;
delete test;
return 0;
}
I seek the wisdom of the opinions of those more experienced than I,
and I thank you for them.