Vector whose elements have function pointer type.

I totally do not understand this exercise.

"Write a declaration for a function that takes two int parameters and returns an int, and declare a vector whose elements have this function pointer type."

Can anyone toss me a bone please?
closed account (D80DSL3A)
Tossing one bone:
std::vector< int(*)(int,int) > funcVec;
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <vector>
using std::vector;

int func (int x, int y);

int main()
{
    typedef int (*pF)(int x, int y);

    vector<pF> myVec;
}


Is this right?
Last edited on
Ran into another problem trying to do the following exercise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using std::cin;
using std::cout; using std::endl;

int addNum (int x, int y);

int main()
{
    typedef int (*pointF)(int x, int y);

    cout << pointF (0);

    return 0;
}

int addNum (int x, int y)
{
    int sum = x + y;

    return sum;
}


Is there a reason why the function pointer is treating it as a bool? Or am I using this horribly wrong?

Edit:

cout << pointF (10, 5);

Returns an error "expression list treated as compound expression in functional cast [-fpermissive]"
Last edited on
There is no conversion from 'pointer to function' to 'pointer to void'.

A pointer to function can be implicitly converted to bool: false if it is null, true otherwise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>

int plus( int a, int b ) { return a+b ; }
int minus( int a, int b ) { return a-b ; }
int multiplies( int a, int b ) { return a*b ; }

int main()
{
    typedef int function_type( int, int ) ;
    using function_type = int(int,int) ; // the same as above

    using pointer_to_function = function_type* ;
    typedef function_type* pointer_to_function ; // alternate syntax

    std::vector<pointer_to_function> ptrs{ plus, minus, multiplies, nullptr } ;

    int x = 50, y = 20 ;
    for( auto pfn : ptrs ) if(pfn) std::cout << pfn(x,y) << '\n' ;

    std::cout << std::boolalpha ;
    for( bool b : ptrs ) std::cout << b << ' ' ; // true true true false
}


http://liveworkspace.org/code/1Vwm9y$0
Okay I think I get part of it.

So function pointers (I think that's the right term?) finds the function to point to by looking at the return type and number of parameters and its types? So the name of the functions are pretty much disregarded?

I still don't understand though why in the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>

int plus( int a, int b ) { return a+b ; }
int minus( int a, int b ) { return a-b ; }
int multiplies( int a, int b ) { return a*b ; }

int main()
{
    typedef int function_type( int, int ) ;
    using function_type = int(int,int) ; // the same as above

    using pointer_to_function = function_type* ;
    typedef function_type* pointer_to_function ; // alternate syntax

    int x = 10, y = 5;

    std::cout << pointer_to_function (plus (x,y)); 

}


1
2
int x = 10, y = 5;
std::cout << pointer_to_function (plus (x,y));


Keeps returning 1, and;

1
2
int x = 10, y = 5;
std::cout << pointer_to_function (x,y);


Has the error "expression list treated as compound expression in functional cast [-fpermissive]".

I know it's dumb, I could just call the function directly instead. It'll be great if you could elaborate a little more though. I don't understand what you mean by:

A pointer to function can be implicitly converted to bool: false if it is null, true otherwise.


Also that leaves me wondering, what are the main uses of function pointers? Appreciate the help and thanks for reading all that rabble.
> what are the main uses of function pointers?

A pointer to function can point to different functions of the right type.
Just as a pointer to int can point to different objects of type int.
Or a pointer to double can point to different objects of type double.

See: http://www.cprogramming.com/tutorial/function-pointers.html

(Though we won't find much use for them much in well-written C++ code)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

int plus( int a, int b ) { return a+b ; }
int minus( int a, int b ) { return a-b ; }
int multiplies( int a, int b ) { return a*b ; }

int i = 10, j = 20, k = 30 ;

int main()
{
    using function_type = int(int,int) ;
    using pointer_to_function = function_type* ;
    using pointer_to_int = int* ;

    pointer_to_function pfn = nullptr ;
    // pfn can point to any function of type int(int,int) (or it can be null)

    pointer_to_int pint = nullptr ;
    // pint can point to any object of type int (or it can be null)

    // pointers are converted to bool; nullptr => false
    std::cout << std::boolalpha << pfn << ' ' << bool(pint) << '\n' ; // false false

    pfn = &plus ; // pfn now points to 'plus'
    pint = &i ; // pint now points to 'i'

    // pointers are converted to bool; non-null pointer => true
    std::cout << pfn << ' ' << bool(pint) << '\n' ; // true true

    // call the function / access the int via pointer
    std::cout << (*pfn)(4,2) << ' ' << *pint << '\n' ; // 6 10 ; plus(4,2) == 6, i == 10

    pfn = plus ; // pfn now points to 'plus' ; &plus is implied
    std::cout << pfn(4,2) << '\n' ; // 6 ( pfn(4,2) => (*pfn)(4,2) ; plus(4,2) == 6

    pfn = minus ; // pfn now points to 'minus'
    pint = &j ; // pint now points to 'j'
    std::cout << pfn << ' ' << bool(pint) << '\n' ; // true true
    std::cout << pfn(4,2) << ' ' << *pint << '\n' ; // 2 20 ; minus(4,2) == 2, j == 20

    pfn = multiplies ; // pfn now points to 'multiplies'
    pint = &k ; // pint now points to 'k'
    std::cout << pfn << ' ' << bool(pint) << '\n' ; // true true
    std::cout << pfn(4,2) << ' ' << *pint << '\n' ; // 8 30 ; multiplies(4,2) == 8, k == 30
}


http://liveworkspace.org/code/37NGNf$0
What I understand from that is that functions have to be pointed at directly to actually have it do what's intended (shallowly speaking).

Leads me to believe that "function pointer" is a pointer that is pointing to function type, not a specific function.

Hope I got it right (at least on a surface level). Thanks JLBorges for taking the time to explain it out, not so confused now.
closed account (D80DSL3A)
I learned some things too.
I got it working simply without all the typedef mumbo-jumbo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>

int add(int a, int b) { return a+b; }
int subtract(int a, int b) { return a-b; }
int multiply(int a, int b) { return a*b; }
int divide(int a, int b) { return a/b; }

int main()
{
    std::vector< int(*)(int,int) > funcVec;

    funcVec.push_back(add);
    funcVec.push_back(subtract);
    funcVec.push_back(multiply);
    funcVec.push_back(divide);

    for(size_t i=0; i < funcVec.size(); ++i)
        std::cout << funcVec[i](5,3) << ' ';

    std::cout << std::endl;
    return 0;
}

Output:
8 2 15 1


EDIT: I hope you don't mind my reply after the thread was marked solved.
Last edited on
Of course not, why would I deny more knowledge? =). Thanks for that, was wondering how it all fits with push_back as well.
Topic archived. No new replies allowed.