Pointer to Member Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahxephon
    New Member
    • Mar 2009
    • 5

    Pointer to Member Function

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

    I have created this
    Code:
    double runge_kutta_4th(double(*equation)(double,double), double initial, double t, double dt){
    	double k1 = equation(initial, t);
    	double k2 = equation(initial + 0.5 * k1 * dt, t + 0.5 *dt);
    	double k3 = equation(initial + 0.5 * k2 * dt, t + 0.5 *dt);
    	double k4 = equation(initial + k3 * dt, t + dt);
    	return initial + (k1 + 2*k2 + 2*k3 + k4)/6 * dt;
    }
    which work fine with a function.

    Now I try to implement it with a equation in a function
    something like this
    Code:
    class equation{
    	public:
    		double a;
    		double b;
    		double c;
    					
    		equation(double a, double b, double c):
    			a(a),
    			b(b),
    			c(c)
    		{
    		}
    
    		~equation(){
    		}
    		
    		double equation1(double y, double t){
    			return (a) * t -  b* y;
    		}
    		
    		double equation2(double y, double t){
    			return 3 * (a) * t -  b* y;
    		}
    };
    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?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Code:
    #include <iostream>
    #include <iomanip>
    
    class MySquare
    {
    public:
    	MySquare(int init=1): multiple(init) {}
        ~MySquare() {}
    
        int operator()(int x) const { return multiple * x * x; }
    
    private:
        int multiple;
    };
    
    template< typename PREDICATE >
    void print(PREDICATE predicate, int input)
    {
    	std::cout << std::setw(3) << input << ": " << predicate(input) << std::endl;
    }
    
    int Sum(int input)
    {
    	int sum = 0;
    
    	for(;input > 0; input--)
    	{
    		sum += input;
    	}
    
    	return sum;
    }
    
    int main()
    {
    	int ix;
    
    	for(ix=0; ix<10; ix++)
    	{
    		print(Sum, ix);
    	}
    
    	for(ix=0; ix<10; ix++)
    	{
    		print(MySquare(5), ix);
    	}
    }

    Comment

    • unauthorized
      New Member
      • May 2009
      • 81

      #3
      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:

      Code:
      class my_class {
      public:
           static int my_method(my_class* pthis)
           {
                return pthis->my_var;
           }
      private:
           int my_var;
      };
      You could also use member function pointers like this:

      Code:
      class my_class {
           void my_method(double a, double b) { ... }
      };
      
      void(class ::*func_ptr)(double, double);
      func_ptr = &my_class::my_method;
      
      my_class obj;
      (obj.*func_ptr)(1.0, 2.0);

      Comment

      • rahxephon
        New Member
        • Mar 2009
        • 5

        #4
        Thanks for both of the reply. I have make used of the functor in my code. Thanks for the example Banfa.

        Comment

        Working...