TICPP CHAPTER 11 EX 27 & EX28
2008-05-23 22:50
162 查看
/*
27. Start with FunctionTable.cpp from Chapter 3. Create a
class that contains a vector of pointers to functions, with
add( ) and remove( ) member functions to add and
remove pointers to functions. Add a run( ) function that
moves through the vector and calls all of the functions.
*/
//: C03:FunctionTable.cpp
#include <iostream>
#include <vector>
using namespace std;
// A macro to define dummy functions:
#define DF(N) void N() { /
cout << "function " #N " called..." << endl; }
DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g);
class FuncTable {
vector<void(*)()>ft;
public:
void add(void(*f)()) {
ft.push_back(f);
}
void remove(){
ft.pop_back();
}
void run() {
for (int i = 0; i < ft.size(); i++) {
//看清楚调用格式 ,有无*号都可以
(*ft[i])();
}
}
};
//void (*func_table[])() = { a, b, c, d, e, f, g };
int main() {
FuncTable test;
test.add(&a);
test.add(&b);
test.run();
test.remove();
cout << "after b() is removed" << endl;
test.run();
} ///:~
//28. Modify the above Exercise 27 so that it works with
//pointers to member functions instead.
//: C03:FunctionTable.cpp
#include <iostream>
#include <vector>
using namespace std;
// A macro to define dummy functions:
#define DF(N) void N() { /
cout << "function X::" #N " called..." << endl; }
#define MAKEX /
class X{ /
public: /
DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g); /
}
//定义了一个X类
MAKEX;
class FuncTable {
vector<void(X::*)()>ft;
public:
void add(void(X::*f)()) {
ft.push_back(f);
}
void remove(){
ft.pop_back();
}
void run(X& x) { //需要传入一个X类对象,否则无法调用
for (int i = 0; i < ft.size(); i++) {
//看清楚调用格式 ,必须有*号
(x.*ft[i])();
}
}
};
//void (*func_table[])() = { a, b, c, d, e, f, g };
int main() {
X x;
FuncTable test;
test.add(&X::a);
test.add(&X::b);
test.run(x);
test.remove();
cout << "after X::b() is removed." << endl;
test.run(x);
} ///:~
27. Start with FunctionTable.cpp from Chapter 3. Create a
class that contains a vector of pointers to functions, with
add( ) and remove( ) member functions to add and
remove pointers to functions. Add a run( ) function that
moves through the vector and calls all of the functions.
*/
//: C03:FunctionTable.cpp
#include <iostream>
#include <vector>
using namespace std;
// A macro to define dummy functions:
#define DF(N) void N() { /
cout << "function " #N " called..." << endl; }
DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g);
class FuncTable {
vector<void(*)()>ft;
public:
void add(void(*f)()) {
ft.push_back(f);
}
void remove(){
ft.pop_back();
}
void run() {
for (int i = 0; i < ft.size(); i++) {
//看清楚调用格式 ,有无*号都可以
(*ft[i])();
}
}
};
//void (*func_table[])() = { a, b, c, d, e, f, g };
int main() {
FuncTable test;
test.add(&a);
test.add(&b);
test.run();
test.remove();
cout << "after b() is removed" << endl;
test.run();
} ///:~
//28. Modify the above Exercise 27 so that it works with
//pointers to member functions instead.
//: C03:FunctionTable.cpp
#include <iostream>
#include <vector>
using namespace std;
// A macro to define dummy functions:
#define DF(N) void N() { /
cout << "function X::" #N " called..." << endl; }
#define MAKEX /
class X{ /
public: /
DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g); /
}
//定义了一个X类
MAKEX;
class FuncTable {
vector<void(X::*)()>ft;
public:
void add(void(X::*f)()) {
ft.push_back(f);
}
void remove(){
ft.pop_back();
}
void run(X& x) { //需要传入一个X类对象,否则无法调用
for (int i = 0; i < ft.size(); i++) {
//看清楚调用格式 ,必须有*号
(x.*ft[i])();
}
}
};
//void (*func_table[])() = { a, b, c, d, e, f, g };
int main() {
X x;
FuncTable test;
test.add(&X::a);
test.add(&X::b);
test.run(x);
test.remove();
cout << "after X::b() is removed." << endl;
test.run(x);
} ///:~
相关文章推荐
- 输入一个整数, 用a-z表示, 相当于从10进制转换到26进制 比如27->aa, 28->ab.
- python学习日记_第十一天(ex27~28)
- Daily Scrum & Project Team Meeting Review - 11/27
- 2012-11-26 - 11-27 11gR2 "Backup and Recovery User's Guide" page 443 - 484
- 心得27--JDK新特性11-注解
- offer题11 数值的整数次方&判断浮点型是否相等&降低求n次方的时间复杂度&3种错误处理方式
- 【起航计划 029】2015 起航计划 Android APIDemo的魔鬼步伐 28 App-&gt;Preferences-&gt;Default Values 偏好默认值
- GCC's bacl-end & assemble emission (27 - continue)
- 如何使用沙箱测试单笔转账到支付宝账号(php版) https://openclub.alipay.com/read.php?tid=1770&fid=28
- 解读ASP.NET 5 & MVC6系列(11):Routing路由
- 11/7/8 STL 映射容器 map< K , T > 学习笔记
- JAVA基础--JAVA API常见对象(字符串&缓冲区)11
- 教育技术资源大全(05-11-28)
- 【小熊刷题】Container with Most Water <Leetcode 11, Java>
- 11-(12&13)-09
- 11/28 前端码农日报
- 13-01-26-->11
- KeyError: "Couldn't get user id for user hue" #27
- 【SSH项目实战】国税协同平台-28.投诉受理需求分析&CDM&PDM
- Chapter 11 Regular Expressions&Assignment