pointers to functions

Posted by DevAno1 on Stack Overflow See other posts from Stack Overflow or by DevAno1
Published on 2010-04-26T19:10:25Z Indexed on 2010/04/26 19:13 UTC
Read the original article Hit count: 368

I have two basic Cpp tasks, but still I have problems with them. First is to write functions mul1,div1,sub1,sum1, taking ints as arguments and returning ints. Then I need to create pointers ptrFun1 and ptrFun2 to functions mul1 and sum1, and print results of using them. Problem starts with defining those pointers. I thought I was doing it right, but devcpp gives me errors in compilation.

#include <iostream>
using namespace std;

int mul1(int a,int b)
{
    return a * b;
}

int div1(int a,int b)
{
    return a / b;    
}

int sum1(int a,int b)
{
    return a + b;   
}

int sub1(int a,int b)
{
    return a - b;    
}


int main()
{
    int a=1;
    int b=5;

    cout << mul1(a,b) << endl;
    cout << div1(a,b) << endl;
    cout << sum1(a,b) << endl;
    cout << sub1(a,b) << endl;

    int *funPtr1(int, int);
    int *funPtr2(int, int);

    funPtr1 = sum1;
    funPtr2 = mul1;

    cout << funPtr1(a,b) << endl;
    cout << funPtr2(a,b) << endl;

    system("PAUSE");
    return 0;
}

38 assignment of function int* funPtr1(int, int)' 38 cannot convertint ()(int, int)' to `int*()(int, int)' in assignment

Task 2 is to create array of pointers to those functions named tabFunPtr. How to do that ?

© Stack Overflow or respective owner

Related posts about c++

Related posts about pointers