fork download
  1. #include <iostream>
  2.  
  3. double foo( double d ) { return d*2 ; }
  4. double bar( double d ) { return d/2 ; }
  5.  
  6. int main()
  7. {
  8. typedef double F(double); // F is an alias for double(double)
  9. // ie. a unary function taking a double and returning a double}
  10.  
  11. F& fn1 = foo ; // type of 'fn1' is reference to F
  12. // ie. a reference to a unary function taking a double and returning a double}
  13. // and it is initialized to refer to foo
  14.  
  15. std::cout << fn1(1.234) << '\n' ; // foo(1.234)
  16.  
  17. F& fn2 = bar ; // type of 'fn2' is reference to F
  18. // ie. a reference to a unary function taking a double and returning a double}
  19. // and it is initialized to refer to bar
  20.  
  21. std::cout << fn2(1.234) << '\n' ; // bar(1.234)
  22.  
  23. F* pfn = nullptr ; // type of 'pfn' is pointer to F
  24. // ie. a pointer to a unary function taking a double and returning a double}
  25. // and it is initialized to be a null pointer
  26.  
  27. pfn = &foo ; // pfn now points to foo
  28. std::cout << (*pfn)(1.234) << '\n' ; // foo(1.234)
  29.  
  30. pfn = &bar ; // pfn now points to bar
  31. std::cout << (*pfn)(1.234) << '\n' ; // bar(1.234)
  32.  
  33. // the above two lines can also be written as:
  34. pfn = bar ; // pfn now points to bar - address of is implied
  35. std::cout << pfn(1.234) << '\n' ; // bar(1.234) - dereference of pointer is implied
  36. }
  37.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
2.468
0.617
2.468
0.617
0.617