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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class ArgumentList
{
private:
vector<void*> arg_list;
public:
ArgumentList & operator<<(void*arg)
{arg_list.push_back(arg);return *this;}
void * operator[](int i)
{return arg_list[i];}
void clear() {arg_list.clear();}
};
class Function
{
public:
string name;
void * address;
virtual void call(ArgumentList&,void*,Function*)=0;
void operator()(ArgumentList&args,void*ret_val,Function*caller)
{call(args,ret_val,caller);}
};
int add2(int a, int c);
int add3(int a, int b, int c);
class Fadd2:public Function
{
public:
Fadd2() {name="int add2(int,int)"; address=(void*)add2;}
void call(ArgumentList&args,void * ret_val,Function*caller);
}fadd2;
class Fadd3:public Function
{
public:
Fadd3() {name="int add3(int,int,int)"; address=(void*)add3;}
void call(ArgumentList&args,void * ret_val,Function*caller);
}fadd3;
int main()
{
int a, b, c;
cout << "enter 3 ints:\n";
cin >> a >> b >> c;
cin.get();
cout << '\n' << a << '+' << b << '+' << c << '=';
ArgumentList args;
int result;
args<<&a<<&b<<&c;
fadd3(args,&result,0);
cout << result << endl;
cout << "\nhit enter to quit...";
cin.get();
return 0;
}
int add2(int a, int b)
{
int result;
result=a+b;
return result;
}
int add3(int a, int b, int c)
{
int result;
ArgumentList args;
args<<&a<<&b;
fadd2(args,&result,&fadd3);
args.clear();
args<<&result<<&c;
fadd2(args,&result,&fadd3);
return result;
}
void Fadd2::call(ArgumentList&args,void * ret_val,Function*caller)
{
//do ANYTHING you want in here...
cout << '\n' << name << " called from " << (caller==0?"main":caller->name) << endl;
static int times_called_from_add3=0;
if(caller->address==add3) times_called_from_add3++;
if(times_called_from_add3==2) cout << "\n(second time called from add3)" << endl;
int * result=(int*)ret_val;
int * arg1=(int*)args[0];
int * arg2=(int*)args[1];
*result=add2(*arg1,*arg2);
cout << "\nreturning from " << name << endl;
}
void Fadd3::call(ArgumentList&args,void * ret_val,Function*caller)
{
//same here...
cout << '\n' << name << " called from " << (caller==0?"main":caller->name) << endl;
int * result=(int*)ret_val;
int * arg1=(int*)args[0];
int * arg2=(int*)args[1];
int * arg3=(int*)args[2];
*result=add3(*arg1,*arg2,*arg3);
cout << "\nreturning from " << name << endl;
}
|