#include<iostream>#include<vector>
using namespace std;int ii =0;//typedef void(*a1)();
template<class T>
class fun
{
private:
vector<T> v;
public:fun();void operator +=(T fa){
v.push_back(fa);}voidRun();};
template <class T> fun<T>::fun(){;}
template <class T>void fun<T>::Run(){for(int i =0; i < v.size(); i++){
T a;
a = v[i];a();}}voida_f(){int a =0;}voidb_f(){int b =0;}intmain(){typedefvoid(*a1)();
fun<a1> f;
fun<a1> f2;
f += a_f;
f += b_f;
f.Run();return0;}
2.链表方式实现
#include<iostream>
class Line
{
private:typedefvoid(*T)(int);typedefstructNode{
Node* last;
T Func;
Node* next;};
Node* node_start;int* Count;
public:Line();void operator +=(const T fun);void operator -=(const T fun);voidRun(int d);~Line();};
Line::Line(){
Count = new int(0);
node_start = new Node();
node_start->last = new Node();
node_start->Func = nullptr;
node_start->next = new Node();}
Line::~Line(){
delete Count;
delete node_start;}void Line::operator +=(T fun){
Node* Next = node_start->next;
Node* Last = new Node();for(int i =0; i <*Count; i++){
Last = Next;
Next = Next->next;}
Next->last = Last;
Next->Func = fun;
Next->next = new Node();(*Count)++;}void Line::operator -=(T fun){
Node* Last = new Node();
Last->last = new Node();
Last->next = new Node();
Last = node_start->next;for(int i =0; i <*Count; i++){if(Last->Func == fun){
Last->last->next = Last->next;
Last->next->last = Last->last;(*Count)--;break;}
Last = Last->next;}
delete Last;}void Line::Run(int d){
Node* next = node_start->next;for(int i =0; i <*Count; i++){
next->Func(d);
next = next->next;}}voidaaa(int a){;}staticint dex =0;voidbbb(int a){
dex++;}intmain(){int* p;
p =(int*)malloc(10*sizeof(int));//申请
p[1]=8;//类似数组的赋值,访问
p[9]=8;free(p);//释放
Line a1;
a1 += aaa;
a1 += bbb;
a1 += bbb;
a1 += bbb;
a1 -= bbb;
a1 -= bbb;
a1 -= bbb;
a1.Run(2);return0;}