signal_return_value.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Example program for returning a value from slots to signal invocation.
  2. //
  3. // Copyright Douglas Gregor 2001-2004.
  4. // Copyright Frank Mori Hess 2009.
  5. //
  6. // Use, modification and
  7. // distribution is subject to the Boost Software License, Version
  8. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. // For more information, see http://www.boost.org
  11. #include <iostream>
  12. #include <boost/signals2/signal.hpp>
  13. //[ signal_return_value_slot_defs_code_snippet
  14. float product(float x, float y) { return x * y; }
  15. float quotient(float x, float y) { return x / y; }
  16. float sum(float x, float y) { return x + y; }
  17. float difference(float x, float y) { return x - y; }
  18. //]
  19. int main()
  20. {
  21. boost::signals2::signal<float (float x, float y)> sig;
  22. //[ signal_return_value_main_code_snippet
  23. sig.connect(&product);
  24. sig.connect(&quotient);
  25. sig.connect(&sum);
  26. sig.connect(&difference);
  27. // The default combiner returns a boost::optional containing the return
  28. // value of the last slot in the slot list, in this case the
  29. // difference function.
  30. std::cout << *sig(5, 3) << std::endl;
  31. //]
  32. }