Return Pointer To Function

I am looking to have one function(called F) return a pointer to one of two other functions(F1 and F2). I do not know how the function prototype for F. I have seen something in c, but it used typedefs. Thanks in advance
Do functions F1 and F2 have the same type?
yes, they both return void and take one int argument
1
2
3
4
5
6
7
8
9
10
void one(int) { /* ... */ }
void two(int) { /* ... */ }

decltype(&one) foo( int v ) { return v < 10 ? one : two ; }

typedef void function_type( int ) ;
function_type* bar( int v ) { return v < 10 ? one : two ; }

#include <functional>
std::function< void(int) > baz( int v ) { return v < 10 ? one : two ; }
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
#include <iostream>
#include <cstdlib>
#include <ctime>

void F1(int i)
{
	std::cout << "F1(" << i << ");" << std::endl;
}

void F2(int i)
{
	std::cout << "F2(" << i << ");" << std::endl;
}


void (*F())(int) 
{
	return (rand() & 1 ? F1 : F2);
}

int main()
{
	std::srand(std::time(0));
	void (*f)(int) = F();
	f(7);
}

Thanks Guys, although could you please explain your code:

@Peter what does
 
void (*F())(int)


do?
@Script Coder
It looks a bit weird when having function pointer as return type. It means that F is a function returning a pointer to a function with return type void taking one int argument.
@Peter Thank You. then why does it have a "void" there?
The void is the return type of the function you return.
Topic archived. No new replies allowed.