8.2(목) C++ - 함수객체의 시작

from Study/C++ 2007/08/02 20:28 view 17915

// void foo( int a )
// {
//      cout << a << endl;
// }

// 함수보다는 함수객체가 훨씬 좋다.

template<typename T> struct Show

{

        ostream& os;

 

        Show( ostream& s = cout ) : os( s ) {}

 

        void operator()( T a )

        {

                os << a << endl;

        }

};

//
Point
foo( Point )
// {
//      Point p;
//      return p;
//      return Point();        // RVO
// }


void
main()

{

//      Point p;

//      foo(p);                // 객체를 만들어서 복사해서 보내기.
//      foo( Point() );        //
만들면서 보내는게 좋다!!

 

        int x[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

//      ofstream f( "a.txt" );

//      Show<int> s(f);

//      for_each( x, x+10, s );

 

        for_each( x, x+10, Show<int>() );

        //for_each( x, x+10, foo );

}

Tag |

Trackback Address :: 이 글에는 트랙백을 보낼 수 없습니다