code_junkie

Custom events in C++?

Nov 10th, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. template <typename T>
  2. class Observable
  3. {
  4. T underlying;
  5.  
  6. public:
  7. Observable<T>& operator=(const T &rhs) {
  8. underlying = rhs;
  9. fireObservers();
  10.  
  11. return *this;
  12. }
  13. operator T() { return underlying; }
  14.  
  15. void addObserver(ObsType obs) { ... }
  16. void fireObservers() { /* Pass every event handler a const & to this instance /* }
  17. };
  18.  
  19. Observable<int> x;
  20. x.registerObserver(...);
  21.  
  22. x = 5;
  23. int y = x;
Add Comment
Please, Sign In to add comment