Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |
Messenger s big brother is the collecting parameter, whose job is to capture information from the function to which it is passed.
Generally, this is used when the collecting parameter is passed to multiple
functions, so it s like a bee collecting pollen.
A container makes an especially useful collecting parameter,
since it is already set up to dynamically add objects:
//: C10:CollectingParameterDemo.cpp
#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;
} ///:~
The collecting parameter must have some way to set or insert
values. Note that by this definition, a messenger could be used as a collecting
parameter. The key is that a collecting parameter is passed about and modified
by the functions that receive it.
Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |