Question

PROBLEM

Suppose I have a class like this:

enum eOutputMode
{
  DECIMAL,
  BILLS_AND_COINS
};

class BankAccount
{
  ssize_t         m_dollars;
  unsigned short  m_pennies;

public:
  friend ostream& operator<<( ostream& os, BankAccount const& rhs )
  {
    // switch on eOutputMode : output in DECIMAL or in BILLS_AND_COINS
    return os;
  }
};

QUESTION

How can I modify the code above so that I can call it like this?

BankAccount  ba;
os << eOutputMode::DECIMAL << ba << "\n";
os << eOutputMode::BILLS_AND_COINS << ba << "\n";

In other words, how and where should I capture the output states?

Was it helpful?

Solution

You can write an output manipulator and capture the states in ios_base::iword (that page has an example of a custom stateful manipulator)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top