Yudan Yi (OSU) wrote:
While I want to add more control to the output by an additional parameter,
can I do in the following way?
No: the 'operator<<()' is a binary function. Assuming you want to
add some form of format control for 'TTest' to your stream, you
can store the format using the 'iword()' or 'pword()' members of
the stream. This could look e.g. like this:
int key = std::ostream::xalloc();
std::ostream& mode1(std::ostream& out) {
out.iword(key) = 1;
return out;
}
std::ostream& mode2(std::ostream& out) {
out.iword(key) = 2;
return out;
}
// ...
friend ostream& operator<<(ostream& os, TTest const& x)
{
switch (os.iword(key))
{
case 1: // ...
case 2: // ...
case 0: // this is the initial value of 'os.iword(key)'
default: // ...
}
return out;
}
You would use it like this:
void print(TTest const& x) {
std::cout << "default format: " << x << "\n";
std::cout << "format 1: " << mode1 << x << "\n";
std::cout << "format 2: " << mode2 << x << "\n";
}
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence