2015. 9. 4. 11:23 Programing/C++
함수 객체
함수 객체는 함수처럼 동작하는 객체, 함수처럼 동작하려면 객체가 '()' 연산자를 정의해야 한다.
즉 () 연산자를 오버로딩한 객체가 함수 객체
#include <iostream>
#include <algorithm>
using namespace std;
struct Functor1 {
void operator() (int n)
{
cout << n << " ";
}
};
struct Functor2 {
void operator() (int n)
{
cout << n*n << " ";
}
};
struct Functor3 {
void operator() (int n)
{
cout << "정수 : " << n << endl;
}
};
int main()
{
int arr[5] = { 10,20,30,40,50 };
for_each(arr, arr + 5, Functor1());
cout << endl << endl;
for_each(arr, arr + 5, Functor2());
cout << endl << endl;
for_each(arr, arr + 5, Functor3());
return 0;
}
'Programing > C++' 카테고리의 다른 글
클래스 템플릿 비교 (0) | 2015.09.04 |
---|---|
두 정수가 같으면 true 다르면 false를 리턴하는 Equal 클래스 (0) | 2015.09.04 |
멤버 함수 포인터 선언 (0) | 2015.09.04 |
형변환 연산자 오버로딩 (0) | 2015.09.04 |
-> , * 연산자 오버로딩 (0) | 2015.09.02 |