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;

}

Posted by kimmayer

블로그 이미지
IT 기술들 정리, 독후감을 주로 남깁니다!
kimmayer

공지사항

Yesterday
Today
Total

달력

 « |  » 2024.3
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함