Thread: function pointers and member functions

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    function pointers and member functions

    I have not been able to find the best (if any satisfactory) solution to the following issue which essentially boils down to how best to have a function (or member function) return a function (pointer).

    More concretely:

    I have an integration function which as one of its parameter takes a funtion of one variable (say 3+2*x), i.e. the parameter has the form double f(double);

    The user can via an interface specify the coefficients to the one parameter function, let's say a and b in the equation a+b*x.

    How do I construct a function which takes two parameters (a and b) as input and returns a funtion (pointer) of one variable, namely a+b*x.

    OR alternatively, how do I construct a class which as it's constructor takes two parameters (a and b) and provide a means for creating and returning a function of one variable, namely a+b*x. If a+b*x is constructed as a member function, it seems I can't use this member function to pass into my integration routine? And if it is somehow possible, would it create a lot of overhead?

    This must be a common sort of problem but I have not been able to find what is a good solution.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    How about this..

    Code:
    class SingleVariableFunction {
    public:
    	virtual float evaulate(float x)=0;
    	~SingleVariableFunction();
    };
    
    class QuadraticFunction : public SingleVariableFunction {
    public:
    	QuadraticFunction(float a, float b, float c); // trivial implmeentation omitted..
    	float evaulate(float x) {
    		return x*(a*x + b) + c;
    	}
    private:
    	float a, b, c;
    };
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    3
    I'm not sure that solves the problem (?)

    I need somehow to get hold of the function which takes one parameter and pass it into my integration routine which has the form
    double trapzd(double func(const double), const double a, const double b, const int n)



    Let qf be an instance of QuadraticFunction.
    I want somehow to get hold of qf.evaluate (which is a function of one variable). But I can't do that since evaluate is a member function and I get the error 'Taking address of the bound function ...'

    How can I do it?

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You don't need function pointers. Use the polymorphic behavior of the evualate method to perform the integration.

    Change the floats to doubles in the signature of both SingleVariableFunction and QuadtraticFunction.

    IE,

    Code:
    #include <iostream>
    
    class SingleVariableFunction {
    public:
     virtual double evaluate(double x)=0;
     // print method might be nice as well
     virtual ~SingleVariableFunction() { }
    };
    
    class QuadtraticFunction: public SingleVariableFunction {
    public:
     QuadtraticFunction(double a_, double b_, double c_) {
      a = a_;
      b = b_;
      c = c_;
     }
     virtual ~QuadtraticFunction() { }
    
     virtual double evaluate(double x) {
      return x*(a*x + b) + c;
     }
    private:
     double a, b, c;
    };
    
    
    double trapezoidalIntegration(SingleVariableFunction* func, double lowerLim, double upperLim, int numIterations) {
     double integralSum=0.0;
     double deltaX = (upperLim - lowerLim) / (double) numIterations;
     double x = lowerLim;
     // can be optimized... only half the calls are really needed, but the loop gets messier and I don't want to think :)
     for ( int i = 0; i < numIterations; i++) {
      integralSum += func->evaluate(x) + func->evaluate(x+ deltaX);
      x+=deltaX;
     }
     integralSum = integralSum * deltaX/ 2;
     return integralSum;
    }
    
    int main() {
     SingleVariableFunction* func = new QuadtraticFunction(1, 0, 0);
     std::cout << "Integral of 1*x^2 from 0 to 2 is " << trapezoidalIntegration(func, 0, 2, 1000) << std::endl;
     delete func;
     return 0;
    }
    Edit:
    Evaluate is the hardest damned word to spell in the English langauge .
    Last edited by SilentStrike; 03-19-2002 at 03:40 PM.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    3
    Thanks for spelling it out for me!

    It exactly does the job.

    In know in Lisp (Scheme) it's very easy and natural to have a function return another function (using the lambda function) but in c++, as you show me, one needs to bring out some of the more heavy machinery.

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Heh.. never used Lisp, heard it's pretty painful though.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About member function pointers...
    By mikahell in forum C++ Programming
    Replies: 10
    Last Post: 07-22-2006, 03:03 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. member member function pointers
    By Bigbio2002 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2005, 05:14 PM
  4. Function Pointers to a Member, from a Member
    By littleweseth in forum C++ Programming
    Replies: 4
    Last Post: 11-01-2003, 02:12 AM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM