Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. class CA
  2. {
  3. public:
  4. CA(void) {};
  5. ~CA(void) {};
  6. void setA(double x) {a = x; };
  7. void setB(double x) {b = x; };
  8.  
  9. double getA(const double x) {return x*a; };
  10. double getB(const double x) {return x*b; };
  11.  
  12. void print(double f(const double), double x) {
  13. char cTemp[256];
  14. sprintf_s(cTemp, "Value = %f", f(x));
  15. std::cout << cTemp;
  16. };
  17.  
  18. private:
  19. double a, b;
  20. };
  21.  
  22. CA cA;
  23. cA.setA(1.0);
  24. cA.setB(2.0);
  25.  
  26. double (*p)(const double);
  27.  
  28. if(true) {
  29. p = &cA.getA; //'&' : illegal operation on bound member function expression
  30. } else {
  31. p = cA.getB; //'CA::getB': function call missing argument list; use '&CA::getB' to create a pointer to member
  32. //'=' : cannot convert from 'double (__thiscall CA::* )(const double)' to 'double (__cdecl *)(const double)'
  33. }
  34.  
  35. cA.print(p, 3.0);
  36.  
  37. // OK for global functions
  38. double (*p)(const double);
  39.  
  40. // OK for member functions
  41. double (CA:*p)(const double);
  42.  
  43. p = &CA::getA;
  44. CA cA;
  45. (cA.*p)();
  46.  
  47. #include <iostream>
  48.  
  49. void print(double (CA::*f)(const double), double x)
  50. {
  51. // Rather use the C++ I/O Library if you can...
  52. std::cout << "Value = " << (this->*f)(x);
  53. };
  54.  
  55. int main()
  56. {
  57. CA cA;
  58. cA.setA(1.0);
  59. cA.setB(2.0);
  60.  
  61. double (CA::*p)(const double);
  62.  
  63. if (true) // Maybe use some more exciting condition :-)
  64. {
  65. p = &CA::getA;
  66. }
  67. else {
  68. p = &CA::getB;
  69. }
  70.  
  71. cA.print(p, 3.0);
  72. }
  73.  
  74. #define CALL_MEMBER_FN(object, ptrToMember) ((object).*(ptrToMember))
  75.  
  76. class CA
  77. {
  78. public:
  79. typedef double (CA::*CAGetter)(const double x);
  80.  
  81. void print(CAGetter f, double x)
  82.  
  83. {
  84. std::cout << "value = " << CALL_MEMBER_FN(*this, f)(x) << 'n';
  85. }
  86.  
  87. CA a;
  88. a.setA(3.1);
  89. a.setB(4.2);
  90.  
  91. // Using a variable...
  92. CA::CAGetter p = &CA::getA;
  93. a.print(p, 1);
  94.  
  95. // without a variable
  96. a.print(&CA::getB, 1);
  97.  
  98. // Calling the functions from outside the class...
  99. std::cout << "From outside (A): " << CALL_MEMBER_FN(a, p)(10) << std::endl;
  100. std::cout << "From outside (B): " << CALL_MEMBER_FN(a, &CA::getB)(10) << std::endl;
  101.  
  102. class CA
  103. {
  104. public:
  105. typedef double (CA::*getter)( double );
  106. CA(void) {};
  107. ~CA(void) {};
  108. void setA(double x) {a = x; };
  109. void setB(double x) {b = x; };
  110.  
  111. double getA(const double x) {return x*a; };
  112. double getB(const double x) {return x*b; };
  113.  
  114. void print(getter f, double x) {
  115. char cTemp[256];
  116. sprintf(cTemp, "Value = %f", (this->*f)(x));
  117. std::cout << cTemp;
  118. };
  119.  
  120. private:
  121. double a, b;
  122. };
  123.  
  124. int main()
  125. {
  126. CA cA;
  127. cA.setA(1.0);
  128. cA.setB(2.0);
  129.  
  130. CA::getter p;
  131.  
  132. if(true) {
  133. p = &CA::getA;
  134. } else {
  135. p = &CA::getB;
  136. cA.print( p, 3.0 );
  137. }
  138.  
  139. class CA
  140. {
  141. public:
  142. typedef boost::function<double( double )> getter;
  143. CA(void) {};
  144. ~CA(void) {};
  145. void setA(double x) {a = x; };
  146. void setB(double x) {b = x; };
  147.  
  148. double getA(const double x) {return x*a; };
  149. double getB(const double x) {return x*b; };
  150.  
  151. void print(getter f, double x) {
  152. char cTemp[256];
  153. sprintf(cTemp, "Value = %f", f(x));
  154. std::cout << cTemp;
  155. };
  156.  
  157. private:
  158. double a, b;
  159. };
  160.  
  161. int main()
  162. {
  163. CA cA;
  164. cA.setA(1.0);
  165. cA.setB(2.0);
  166.  
  167. CA::getter p;
  168.  
  169. if(true) {
  170. p = boost::bind( &CA::getA, &cA, _1 );
  171. } else {
  172. p = boost::bind( &CA::getB, &cA, _1 );
  173. }
  174. cA.print( p, 3.0 );
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement