Results 1 to 6 of 6

Thread: Implementing a function pointer

  1. #1
    Join Date
    Nov 2009
    Location
    Germany
    Beans
    152
    Distro
    Ubuntu 12.04 Precise Pangolin

    Implementing a function pointer

    I'm trying to implement a function pointer in C++ with object-oriented paradigm in mind. However, I didn't get it working. It complaints somehow when I call functionPointer.implement(10, 100, &FunctionPointer::sub).

    Code:
    #include <iostream>
    
    class FunctionPointer
    {
    private:
      int a;
      int b;
    
    public:
      FunctionPointer();
      ~FunctionPointer();
    
      double add(int a, int b){return a+b;}
      double sub(int a, int b){return a-b;}
      double mul(int a, int b){return a*b;}
      double div(int a, int b){return a/b;}
      void implement(int a, int b, double(*func)(int, int));
    
    };
    
    FunctionPointer::FunctionPointer()
    {
    
    }
    
    FunctionPointer::~FunctionPointer()
    {
    
    }
    
    void FunctionPointer::implement(int a, int b, double(*func)(int, int))
    {
      double result;
      result = func(a, b);
      std::cout << "result = " << result << std::endl;
    }
    
    
    int main()
    {
      std::cout << "hello" << std::endl;
    
      FunctionPointer functionPointer;
      functionPointer.implement(10, 100, &FunctionPointer::sub);
    
      return 0;
    
    }
    Any idea how to fix it. Thanks in advance.

  2. #2
    Join Date
    Apr 2005
    Location
    Hampshire, UK
    Beans
    1,273

    Re: Implementing a function pointer

    Make add, sub, div, mul and implement static.

    Code:
    #include <iostream>
    
    class FunctionPointer
    {
    private:
      int a;
      int b;
    
    public:
      FunctionPointer();
      ~FunctionPointer();
    
      static double add(int a, int b){return a+b;}
      static double sub(int a, int b){return a-b;}
      static double mul(int a, int b){return a*b;}
      static double div(int a, int b){return a/b;}
      static void implement(int a, int b, double(*func)(int, int));
    
    };
    
    FunctionPointer::FunctionPointer()
    {
    
    }
    
    FunctionPointer::~FunctionPointer()
    {
    
    }
    
    void FunctionPointer::implement(int a, int b, double(*func)(int, int))
    {
      double result;
      result = func(a, b);
      std::cout << "result = " << result << std::endl;
    }
    
    
    int main()
    {
      std::cout << "hello" << std::endl;
    
      FunctionPointer::implement(10, 100, &FunctionPointer::sub);
    
      return 0;
    
    }
    Last edited by GeneralZod; April 4th, 2012 at 07:35 PM.

  3. #3
    Join Date
    Nov 2009
    Location
    Germany
    Beans
    152
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Implementing a function pointer

    Quote Originally Posted by GeneralZod View Post
    Make add, sub, div and mul static.
    Why those methods should be made/declared static? I didn't get it.

  4. #4
    Join Date
    Apr 2005
    Location
    Hampshire, UK
    Beans
    1,273

    Re: Implementing a function pointer

    Quote Originally Posted by alfa_80 View Post
    Why those methods should be made/declared static? I didn't get it.
    Because otherwise they are member functions which are not interchangable with ordinary functions.

    See e.g.

    http://www.parashift.com/c++-faq-lit....html#faq-33.6

  5. #5
    Join Date
    Nov 2009
    Location
    Germany
    Beans
    152
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Implementing a function pointer

    Quote Originally Posted by GeneralZod View Post
    Because otherwise they are member functions which are not interchangable with ordinary functions.

    See e.g.

    http://www.parashift.com/c++-faq-lit....html#faq-33.6

    Thanks a lot for the great explanation.

  6. #6
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,239
    Distro
    Kubuntu

    Re: Implementing a function pointer

    Quote Originally Posted by alfa_80 View Post
    Any idea how to fix it. Thanks in advance.
    One option available to you is to employ the "boost" C++ library. For example:
    Code:
    #include <boost/bind.hpp>
    #include <string>
    #include <sstream>
    #include <iostream>
    
    struct X
    {
        int add(int a, int b) { return a + b; }
        int sub(int a, int b) { return a - b; }
        int mul(int a, int b) { return a * b; }
        int div(int a, int b) { return a / b; }
        int err(int a, int b) { throw int(-1); }
    };
    
    
    int main()
    {
        // Gather input from the user (note, there is no error checking)
        //
        std::string input;
        char        op;
        int         num1, num2;
    
        std::cout << "Enter first number: ";
        std::getline(std::cin, input);
        std::istringstream iss(input);
        iss >> num1;
    
        std::cout << "Enter an operator (ie. +, -, *, or /): ";
        std::getline(std::cin, input);
        op = input[0];
    
        std::cout << "Enter second number: ";
        std::getline(std::cin, input);
        iss.clear();
        iss.str(input);
        iss >> num2;
    
    
        // Setup pointer to desired function based on what the user inputted.
        //
        int (X::*func)(int, int);
    
        switch (op)
        {
            case '+' : func = &X::add; break;
            case '-' : func = &X::sub; break;
            case '*' : func = &X::mul; break;
            case '/' : func = &X::div; break;
            default  : func = &X::err; break;
        }
    
    
        // Instantiate an object of type X
        //
        X x;
    
    
        // We bind function 'func' to be callable using a reference to
        // our object 'x' (thus obviating the need to make the class
        // functions static), and this function will accept two parameters,
        // those being _1 and _2.  We then call the function, passing num1
        // and num2, and then save our result.
        //
        int result = boost::bind(func, boost::ref(x), _1, _2)(num1, num2);
    
        std::cout << result << std::endl;
    }

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •