如何让一个函数返回一个指向一个函数的指针? (C ++) [英] How to make a function return a pointer to a function? (C++)
本文介绍了如何让一个函数返回一个指向一个函数的指针? (C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图创建一个函数,它接受一个字符,然后返回一个指向函数的指针,取决于字符是什么。我只是不知道如何让一个函数返回一个指向一个函数的指针。
I'm trying to make a function that takes a character, then returns a pointer to a function depending on what the character was. I just am not sure how to make a function return a pointer to a function.
推荐答案
#include <iostream>
using namespace std;
int f1() {
return 1;
}
int f2() {
return 2;
}
typedef int (*fptr)();
fptr f( char c ) {
if ( c == '1' ) {
return f1;
}
else {
return f2;
}
}
int main() {
char c = '1';
fptr fp = f( c );
cout << fp() << endl;
}
这篇关于如何让一个函数返回一个指向一个函数的指针? (C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文