Thread: references to function in classes

  1. #1
    Registered User
    Join Date
    Oct 2007

    Unhappy references to function in classes

    How can I get this one work?
    There is a class which gets a function f as an argument. This function is combined with other ones to a new function diff. So far it's fine.

    Now I want to pass this new function as a reference for further usage. I just can't get around this error the compiler gives me: " cannot convert ‘double (test::*)(double)’ to ‘double (*)(double)’ in assignment "

    I even don't understand the problem at all.

    //provides a function (diff) which gives the difference from an inputfuntion to zero.
    class test
      test() {diff = &standardFunc_;};
       double (*diff)(double x);
       void setFunc(double f(double x))
        func_ = f;
        diff = &test::calc_; //error:  cannot convert ‘double (test::*)(double)’ to ‘double (*)(double)’ in assignment 
      static double standardFunc_(double x) { return 0;}
      double (*func_)(double x);
      double calc_(double x)
        return func_(x) - standardFunc_(x);
    I need that for ceating an easy-to-use fit-interface for the gsl-library. For fitting I need to give some functions as refrences - but this doesn't work within a class .

    Hope somebody can help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Declaring function pointers to class member functions is a bit more involved than what you have at the moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2007

    references to function in classes

    thanks, i see... ok, i will read this Tutorial.

    Is there anybody who knows and wants to post an example how to use this class-function instead of any global (static) function?


  4. #4
    Registered User
    Join Date
    Oct 2007


    Thank you for help and suggestions.

    I don't want to declare the method static, because it should be different for every object. Anyway, I've found a solution.

    For the given example:
    I declare a static vector of pointers to methods func_. There every object gets its own entry. Then, instead of using the method diff, I use calc_ with the appropriate method func_.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Inheritance Classes w/ same function name
    By Diamonds in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2003, 01:11 AM