C++ Primer exercise section 6.7

Exercise 6.54: 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 someone please write a solution or atleast help explaining this to me?
do you know "vector" and "pointer to function" ?
Yes, It's just that I don't understand the question that well. I'm not native english speaker.
declare a vector whose elements are function pointers.
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
//store function pointer in vector // anup jan 6, 2015
#include <iostream>
#include <vector>
using namespace std;

bool isGreater(int a, double b)
{	
	return a>b ? true:false;
}

bool isEqual (int a, double b)
{	
	return a==b ? true:false;
}

int main() {

	bool(*ptf)(int,double) = isGreater; // ptf is a pointer to function
  
	cout << ptf(10,12) << endl;

	vector<bool(*)(int,double)> vect(10);  // 10 initial elements
	
	vect[0] = isEqual;
	cout << vect[0](2, 2.0) << endl;

	vect.push_back( ptf ); // in vect[10]
	cout << vect[10](4, 3.5) << endl; //same as, cout << ptf(5,3) << endl;

	vect.push_back( (bool(*)(int,double)) isEqual ); // in vect[11]	
	 
	if(vect[11](2, 1.999999))  cout << "they are equal\n";
	else  cout << "they are not equal\n";
	
return 0;
}


as you can see from the example different elements can point to different functions. but the functions must have same return type and parameter list.
Topic archived. No new replies allowed.