理解指向函数的指针
2013-04-02 14:07
239 查看
关于指向函数的指针的理解
1、bool (*pf)(const &string const &string)//这条语句将pf声明为指向一个函数类型的指针,就像是指向一个int类型的声明一样的。
该函数类型定义为,带有两个const &string形参并返回一个bool型的变量。
*pf两侧的括号是必须要加上的,不能遗忘。否则就会发生完全不同的两回事情,哈哈。
bool *pf(const &string const &string)//他声明一个名字为pf的函数,返回一个bool型的指针变量
2、使用typedef简化函数指针的定义
typedef bool (*cmpFcn)(const string &, const string &);
cmpFcn f1;//f1是一个指向函数的指针
3、函数指针的初始化
bool lengthCompare(const string &, const string &);
cmpFcn pf1 = lengthCompare;
cmpFcn pf2 = &lengthCompare;
看到没,使用取地址符和不使用取地址符是一样的ok
4、使用指针调用函数
cmpFcn pf = lengthCompare;
lengthCompare("hi", "bye"); // direct call
pf("hi", "bye"); // equivalent call: pf1 implicitly dereferenced
(*pf)("hi", "bye"); // equivalent call: pf1 explicitly dereferenced
看到没,使用解引用和不适用接引用都是一样的ok
5、使用函数指针作为形参的方法
void useBigger(const string &, const string &, bool(const string &, const string &)); // equivalent declaration: explicitly define the parameter as a pointer to function
void useBigger(const string &, const string &, bool (*)(const string &, const string &));
6、理解函数返回指向函数的指针变量
int (*ff(int))(int*, int); //他声明了一个函数ff(int),并且该函数返回一个指向int (*)(int *,int)类型函数的指针。
@看起来很麻烦是不是,可以使用typedef进行简化
typedef int (*pf)(int *,int);
pf ff(int)//是不是更容易理解,嘻嘻
7、指向重载函数的指针
指向重载函数的指针必须与一个重载函数的版本精确匹配,否则,初始化或者赋值后会造成编译错误。
1、bool (*pf)(const &string const &string)//这条语句将pf声明为指向一个函数类型的指针,就像是指向一个int类型的声明一样的。
该函数类型定义为,带有两个const &string形参并返回一个bool型的变量。
*pf两侧的括号是必须要加上的,不能遗忘。否则就会发生完全不同的两回事情,哈哈。
bool *pf(const &string const &string)//他声明一个名字为pf的函数,返回一个bool型的指针变量
2、使用typedef简化函数指针的定义
typedef bool (*cmpFcn)(const string &, const string &);
cmpFcn f1;//f1是一个指向函数的指针
3、函数指针的初始化
bool lengthCompare(const string &, const string &);
cmpFcn pf1 = lengthCompare;
cmpFcn pf2 = &lengthCompare;
看到没,使用取地址符和不使用取地址符是一样的ok
4、使用指针调用函数
cmpFcn pf = lengthCompare;
lengthCompare("hi", "bye"); // direct call
pf("hi", "bye"); // equivalent call: pf1 implicitly dereferenced
(*pf)("hi", "bye"); // equivalent call: pf1 explicitly dereferenced
看到没,使用解引用和不适用接引用都是一样的ok
5、使用函数指针作为形参的方法
void useBigger(const string &, const string &, bool(const string &, const string &)); // equivalent declaration: explicitly define the parameter as a pointer to function
void useBigger(const string &, const string &, bool (*)(const string &, const string &));
6、理解函数返回指向函数的指针变量
int (*ff(int))(int*, int); //他声明了一个函数ff(int),并且该函数返回一个指向int (*)(int *,int)类型函数的指针。
@看起来很麻烦是不是,可以使用typedef进行简化
typedef int (*pf)(int *,int);
pf ff(int)//是不是更容易理解,嘻嘻
7、指向重载函数的指针
指向重载函数的指针必须与一个重载函数的版本精确匹配,否则,初始化或者赋值后会造成编译错误。
相关文章推荐
- 深入理解 [指针函数] 、[函数指针]、[指针的指针]、[指向指针数组的指针]
- 理解 指针数组 数组指针 函数指针 函数指针数组 指向函数指针数组的指针
- 指针数组、数组指针、函数指针、函数指针数组、指向函数指针数组的指针的理解
- 对指针数组 ; 数组指针 ; 函数指针 ; 函数指针数组 ; 指向函数指针数组的指针的理解
- 指针数组 ,数组指针 ,函数指针 ,函数指针数组 ,指向函数指针数组的指针 的理解
- 深入理解C语言中的指向函数的指针!
- 【C语言基础】对指针数组,数组指针,函数指针, 函数指针数组,指向函数指针数组的指针的理解
- 深入理解C语言中的指向函数的指针和返回指针的函数!
- 对于数组指针—指针数组;函数指针—函数指针数组,指向函数指针数组的指针,的理解
- 【笔试面试知识点查缺补漏深入理解之C与C++篇】指向函数的指针
- 理解 指针数组 数组指针 函数指针 函数指针数组 指向函数指针数组的指针
- 理解指针数组、数组指针、函数指针、函数指针数组、指向函数指针数组的指针
- 理解 指针数组 数组指针 函数指针 函数指针数组 指向函数指针数组的指针
- C基础知识之理解指向函数的指针
- 【C】理解指针数组、数组指针、函数指针、函数指针数组、指向函数指针数组的指针
- 什么是指针?关于指针的数组,数组指针,函数指针,函数指针数组,指向函数指针数组的指针的理解
- 指针数组 数组指针 函数指针 函数指针数组 指向函数指针数组的指针的理解
- 关于函数指针的理解(指向的函数返回值又是另外一个函数指针)
- int * (*ptr)()怎么理解(指向函数的指针)
- 深入理解指针数组、数组指针、函数指针、函数指针数组、指向函数指针数组的指针