473,412 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Pointer to Member Function

Trying to built a program but meet some problem in it about the pointer to use.

I have created this
Expand|Select|Wrap|Line Numbers
  1. double runge_kutta_4th(double(*equation)(double,double), double initial, double t, double dt){
  2.     double k1 = equation(initial, t);
  3.     double k2 = equation(initial + 0.5 * k1 * dt, t + 0.5 *dt);
  4.     double k3 = equation(initial + 0.5 * k2 * dt, t + 0.5 *dt);
  5.     double k4 = equation(initial + k3 * dt, t + dt);
  6.     return initial + (k1 + 2*k2 + 2*k3 + k4)/6 * dt;
  7. }
  8.  
which work fine with a function.

Now I try to implement it with a equation in a function
something like this
Expand|Select|Wrap|Line Numbers
  1. class equation{
  2.     public:
  3.         double a;
  4.         double b;
  5.         double c;
  6.  
  7.         equation(double a, double b, double c):
  8.             a(a),
  9.             b(b),
  10.             c(c)
  11.         {
  12.         }
  13.  
  14.         ~equation(){
  15.         }
  16.  
  17.         double equation1(double y, double t){
  18.             return (a) * t -  b* y;
  19.         }
  20.  
  21.         double equation2(double y, double t){
  22.             return 3 * (a) * t -  b* y;
  23.         }
  24. };
A bit stuck on how to pass the equation to the receiving function. Anyone have any idea? Might have to pass equation 1 on some time and pass equation to on another time. Or should i implement the solution in the class rather than calling it externally?
Aug 26 '09 #1
3 1965
Banfa
9,065 Expert Mod 8TB
If you want to pass you class to your function double runge_kutta_4th you need to define you class as a functor. That is a class with the operator() implemented.

You will also need to change the called function a bit. The following is an example of a function that can take a predicate that is either a function or a functor.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. class MySquare
  5. {
  6. public:
  7.     MySquare(int init=1): multiple(init) {}
  8.     ~MySquare() {}
  9.  
  10.     int operator()(int x) const { return multiple * x * x; }
  11.  
  12. private:
  13.     int multiple;
  14. };
  15.  
  16. template< typename PREDICATE >
  17. void print(PREDICATE predicate, int input)
  18. {
  19.     std::cout << std::setw(3) << input << ": " << predicate(input) << std::endl;
  20. }
  21.  
  22. int Sum(int input)
  23. {
  24.     int sum = 0;
  25.  
  26.     for(;input > 0; input--)
  27.     {
  28.         sum += input;
  29.     }
  30.  
  31.     return sum;
  32. }
  33.  
  34. int main()
  35. {
  36.     int ix;
  37.  
  38.     for(ix=0; ix<10; ix++)
  39.     {
  40.         print(Sum, ix);
  41.     }
  42.  
  43.     for(ix=0; ix<10; ix++)
  44.     {
  45.         print(MySquare(5), ix);
  46.     }
  47. }
  48.  
Aug 26 '09 #2
If you don't mind passing around an extra pointer, you can also declare a static method with the first parameter being a pointer or reference to your class. A static member still can access private data through a pointer/reference to it's class but the syntax is a little ugly. It goes like this:

Expand|Select|Wrap|Line Numbers
  1. class my_class {
  2. public:
  3.      static int my_method(my_class* pthis)
  4.      {
  5.           return pthis->my_var;
  6.      }
  7. private:
  8.      int my_var;
  9. };
You could also use member function pointers like this:

Expand|Select|Wrap|Line Numbers
  1. class my_class {
  2.      void my_method(double a, double b) { ... }
  3. };
  4.  
  5. void(class ::*func_ptr)(double, double);
  6. func_ptr = &my_class::my_method;
  7.  
  8. my_class obj;
  9. (obj.*func_ptr)(1.0, 2.0);
Aug 26 '09 #3
Thanks for both of the reply. I have make used of the functor in my code. Thanks for the example Banfa.
Aug 27 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: David Hill | last post by:
Hello - I am using a library that takes a function pointer as an argument. Is the code below not possible? int library_func(void (*func)(int, short, void *)); I am trying to do this... ...
5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.