일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 쿠팡파트너스
- 그누보드
- 쿠팡자동포스팅
- 무료일러스트
- 웹
- cocos2d-x
- 워드프레스
- 파이썬
- 쿠팡 블로그
- 챗지피티
- 아두이노 기초강좌
- 자동글쓰기
- 아두이노 초보
- 쿠파스
- Javascript
- 프로그램
- 자동포스팅
- Python
- 파싱
- php
- 아두이노 강좌
- HTML
- flex
- 자동블로그
- c#
- Ajax
- 자바스크립트
- 아두이노
- Php공부
- ChatGPT
Archives
- Today
- Total
플밍
간단한 클래스멤버함수 콜백 본문
C++에서 클래스멤버함수포인터를 직접 쓰는 대신 c++11에서 추가된 std::function 으로 간단하게 콜백을 구현할 수 있다.
(근데 이게 cygwin에서는 안먹히네요. 다 뜯어고쳤잖아잉~ ㅠㅠ)
굵은색을 주의깊게 살펴보자
#ads_1
#include <iostream>
#include <functional>
using namespace std;
class Child{
private:
typedef std::function<void()> func1; //인수가 없는것
typedef std::function<void(int, int)> func2; //인수가 있는것
func1 onHandler1;
func2 onHandler2;
public:
void setHandler1(func1 f){
onHandler1 = std::move(f);
}
void setHandler2(func2 f){
onHandler2 = std::move(f);
}
void test(){
onHandler1();
onHandler2(30, 20);
}
};
class Test{
public:
void exec(){
Child child;
child.setHandler1(std::bind(&Test::callfunc1, this));
child.setHandler2(std::bind(&Test::callfunc2, this, std::placeholders::_1, std::placeholders::_2)); //_1, _2는 인수
child.test();
}
void callfunc1(){
cout << "callfunc1" << endl;
}
void callfunc2(int n1, int n2){
cout << "callfunc2: " << n1 << ", " << n2 << endl;
}
};
int main(){
Test t;
t.exec();
return 0;
}
#include <functional>
using namespace std;
class Child{
private:
typedef std::function<void()> func1; //인수가 없는것
typedef std::function<void(int, int)> func2; //인수가 있는것
func1 onHandler1;
func2 onHandler2;
public:
void setHandler1(func1 f){
onHandler1 = std::move(f);
}
void setHandler2(func2 f){
onHandler2 = std::move(f);
}
void test(){
onHandler1();
onHandler2(30, 20);
}
};
class Test{
public:
void exec(){
Child child;
child.setHandler1(std::bind(&Test::callfunc1, this));
child.setHandler2(std::bind(&Test::callfunc2, this, std::placeholders::_1, std::placeholders::_2)); //_1, _2는 인수
child.test();
}
void callfunc1(){
cout << "callfunc1" << endl;
}
void callfunc2(int n1, int n2){
cout << "callfunc2: " << n1 << ", " << n2 << endl;
}
};
int main(){
Test t;
t.exec();
return 0;
}
<< 결과 >>
callfunc1
callfunc2: 30, 20
#ads_1
Comments