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 46 47 48
| #include <iostream> #include <string>
using std::string; using std::cout; using std::endl;
int lengthCompare(const string &, const string &);
int(*ff(int))(int*, int);
int f(int *, int);
int main() { typedef int(*compFcn)(const string &, const string &); compFcn pf = lengthCompare; int flag = lengthCompare("hi", "hello"); cout << "compare result : " << flag << endl; flag = pf("hello", "hello"); cout << "compare result : " << flag << endl; flag = (*pf)("hello", "hi"); cout << "compare result : " << flag << endl; int i = 1; int(*fp)(int*, int) = ff(i); int r = fp(&i, i); cout << "最终结果:" << r << endl; cout << "最终结果,一步到位:" << ff(i)(&i,i) << endl; return 0; }
int lengthCompare(const string &s1, const string &s2) { return s1.size() - s2.size(); }
int(*ff(int a))(int * p, int i) { cout << "用参数a做点什么:" << a << endl; int(*fp)(int*, int) = f; return fp; }
int f(int * p, int i) { return (*p) + i; }
|