标签:
#include <functional> #include <vector> #include <algorithm> #include <iostream> typedef std::function<void(int)> Func; class Observer { public: void AddObserver(Func func) { m_vecFuncs.push_back(func); } void Notify(int i) { std::for_each(m_vecFuncs.begin(), m_vecFuncs.end(), [=](Func func){func(i); } ); } private: std::vector<Func> m_vecFuncs; }; class Add { public: void operator()(int i, int b=0) { std::cout << i << std::endl; } }; class Sub { public: void func(int i) { std::cout << i << std::endl; } }; int main() { Observer observer; observer.AddObserver(Add()); Sub sub; observer.AddObserver(std::bind(&Sub::func, &sub, std::placeholders::_1)); observer.Notify(1); observer.Notify(2); }
标签:
原文地址:http://www.cnblogs.com/nmgxbc/p/5638367.html