문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top