📄 applist.cpp
字号:
//: C16:Applist.cpp
// From Thinking in C++, 2nd Edition
// at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// Apply a function to a TStack
#include <iostream>
#include "TStack.h"
using namespace std;
// 0 arguments, any type of return value:
template<class T, class R>
void applist(TStack<T>& tl, R(T::*f)()) {
TStackIterator<T> it(tl);
while(it) {
(it.current()->*f)();
it++;
}
}
// 1 argument, any type of return value:
template<class T, class R, class A>
void applist(TStack<T>& tl, R(T::*f)(A), A a) {
TStackIterator<T> it(tl);
while(it) {
(it.current()->*f)(a);
it++;
}
}
// 2 arguments, any type of return value:
template<class T, class R, class A1, class A2>
void applist(TStack<T>& tl, R(T::*f)(A1, A2),
A1 a1, A2 a2) {
TStackIterator<T> it(tl);
while(it) {
(it.current()->*f)(a1, a2);
it++;
}
}
// Etc., to handle maximum probable arguments
class Gromit { // The techno-dog
int arf;
public:
Gromit(int Arf = 1) : arf(Arf + 1) {}
void speak(int) {
for(int i = 0; i < arf; i++)
cout << "arf! ";
cout << endl;
}
char eat(float) {
cout << "chomp!" << endl;
return 'z';
}
int sleep(char, double) {
cout << "zzz..." << endl;
return 0;
}
void sit(void) {}
};
int main() {
TStack<Gromit> dogs;
for(int i = 0; i < 5; i++)
dogs.push(new Gromit(i));
applist(dogs, &Gromit::speak, 1);
applist(dogs, &Gromit::eat, 2.0f);
applist(dogs, &Gromit::sleep, 'z', 3.0);
applist(dogs, &Gromit::sit);
} ///:~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -