Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- JavaScript
- window
- install
- channel
- Python
- 책
- 리뷰
- bitcoin
- Golang
- GO 언어
- mutex
- http
- Linux
- write
- FOR
- C
- Close
- range
- tcp
- Callback
- windows
- API
- json
- 영화
- Sync
- go
- File
- c++
- package
- go언어
Archives
- Today
- Total
Code Habit
[c++] std::function, std::bind 사용하여 Callback 로직 구현하기 본문
c++ 11 이상부터는 std::function, std::bind 사용하여 callback 함수를 묶어 호출하는 패턴을 사용할 수 있다.
class CallbackClass {
public:
void Callbackfunction(int x) {
std::cout<< "Callback Func Called : " << x << std::endl;
}
}
void CallbackFunction2(int x, int y) {
std::cout << "Callback Func Called : " << x << std::endl;
}
int main() {
CallbackClass cls;
std::function<void(int)> cbMember = std::bind(&CallbackClass::Callbackfunction, &cls, std::placeholders::_1);
std::function<void(int)> cbFunc = std::bind(&CallbackFunction2, std::placeholders::_1, std::placeholders::_2);
cbMemeber(1);
cbFunc(2, 3);
return 0;
}
member 함수를 bind 할 때는 클래스 object(&cls)를 매개변수로 전달해야 하고 std::placeholders::_1은 콜백함수로 전달되는 매개변수를 의미한다.