Some of you might know that I am re-learning myself C and C++. Why? Because C and C++ are the most powerful computer languages on the planet.
After chatting to a work colleague, Siegfried, I decided to try to implement delegate-like functionality in C. I am very happy to say that my small test program is a complete success.
Here is the code:
#include
#include
using namespace std;
int main();
void myfunc();
void myfunc2();
void myfunc3();
class CDelegator
{
private:
std::vector _functions;
public:
void AddFunctionPtr(void (*fn)())
{
_functions.push_back(fn);
}
void AddFunction(void (fn)())
{
_functions.push_back(*fn);
}
void Fire()
{
vector::iterator iter;
for (iter = _functions.begin();
iter<_functions.end();
iter++)
{
cout << &*iter << endl;
void(*func)();
func = *iter;
func();
}
}
};
int main()
{
CDelegator delegator;
delegator.AddFunction(myfunc);
delegator.AddFunction(myfunc2);
delegator.AddFunction(myfunc3);
delegator.Fire();
return 0;
}
void myfunc()
{
cout << "MyFunc" << endl;
}
void myfunc2()
{
cout << "MyFunc2" << endl;
}
void myfunc3()
{
cout << "MyFunc3" << endl;
}