다른 하나-출력 스트림에 Manipulators 인수

다음 예제에서는 클래스를 사용 하 여 money, 된는 long 형식입니다.setpic Manipulator 첨부 서식 "그림" 문자열 클래스의 오버 로드 된 스트림에 삽입 연산자에서 사용할 수 있는 클래스에 money.지정 문자열의 정적 변수로 저장 되는 money stream 클래스의 데이터 멤버로 지금 새 출력 스트림 클래스를 파생 시키는 되지 않은 것이 아니라 클래스입니다.

예제

// one_arg_output.cpp
// compile with: /GR /EHsc
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

typedef char* charp;

class money 
{
private:
    long value;
    static char *szCurrentPic;
public:
    money( long val ) { value = val; }
    friend ostream& operator << ( ostream& os, money m ) {
        // A more complete function would merge the picture
        // with the value rather than simply appending it
        os << m.value << '[' << money::szCurrentPic << ']';
        return os;
    }
    static void setpic( char* szPic ) {
        money::szCurrentPic = new char[strlen( szPic ) + 1];
        strcpy_s( money::szCurrentPic, strlen( szPic ) + 1, szPic );
    }
};

char *money::szCurrentPic;  // Static pointer to picture

void fb( ios_base& os, char * somename )
{
   money::setpic(somename);
/*
   ostream *pos = dynamic_cast<ostream*>(&os);
   if (pos)
   {
      for( int i=0; i < l; i++ )
      (*pos) << ' ';
   };
*/
}

_Smanip<charp>
   __cdecl setpic(char * somename)
   {   
   return (_Smanip<charp>(&fb, somename));
   }

int main( )
{
    money amt = (long)35235.22;
    cout << setiosflags( ios::fixed );
    cout << setpic( "###,###,###.##" ) << "amount = " << amt << endl;
}

참고 항목

참조

사용자 지정 Manipulators 인수