信使的大兄弟是收集参数 Collecting Parameter
工作就是从传递给它的函数中获取信息
通常,收集参数被传递给多个函数的时候使用它
就像蜜蜂在采集花粉一样
//: C10:CollectingParameterDemo.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class CollectingParameter : public vector<string> {};
class Filler {
public:
void f(CollectingParameter& cp) {
cp.push_back("accumulating");
}
void g(CollectingParameter& cp) {
cp.push_back("items");
}
void h(CollectingParameter& cp) {
cp.push_back("as we go");
}
};
int main() {
Filler filler;
CollectingParameter cp;
filler.f(cp);
filler.g(cp);
filler.h(cp);
vector<string>::iterator it = cp.begin();
while(it != cp.end())
cout << *it++ << " ";
cout << endl;
getchar();
} ///:~
输出
accumulating items as we go
收集参数必须有一些方法用来设置值或者插入值
定义信使可以被当做收集参数来使用
问题的关键是收集参数通过接收它的函数进行传递和修改