C++ Primer 5th笔记(6)chapter6 函数:函数指针
生活随笔
收集整理的這篇文章主要介紹了
C++ Primer 5th笔记(6)chapter6 函数:函数指针
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. 函數(shù)指針
bool lengthCompare(const string&, const string&); bool (*pf)(const string&, const string&); pf = lengthCompare; pf = &lengthCompare;2. 重載函數(shù)的指針
bool *pf(const string&, const string&);//表示返回值為bool指針的函數(shù)void f(int*); void ff(unsigned int);void (*pf1)(unsigned in) = ff;void (*pf2)(int) = ff; //error double (*pf2)(int*) = ff;//errorvoid useBigger(const string &s1, const string &s2, bool pf(const string &, const string &));//看似類型 實則指針 void useBigger(const string &s1, const string &s2,bool (*pf)(const string &, const string &));//等價:顯示的聲明 useBigger(s1, s2, lengthCompare);//直接調(diào)用//Func和Fun2函數(shù)類型 typedef bool Func(const string &, const string &); typedef decltype(lengthCompare) Func2; //等價定義//FuncP和FuncP2都是指向函數(shù)的指針 typedef bool (*FuncP)(const string &, const string &); typedef decltype(lengthCompare) *FuncP2; //等價定義void useBigger(const string &s1, const string &s2, Func); void useBigger(const string &s1, const string &s2, Func2); //同上句3. 返回指向函數(shù)的指針
using F= int(int*, int);//F1是函數(shù)類型,不是指針 using PF = int(*)(int*, int);//等價上句 PF f1(int); //正確,PF是指向函數(shù)的指針,類型也正是f1所返回的函數(shù)的類型 F f1(int); //錯誤,F是函數(shù)類型,f1不能返回一個函數(shù) F* f1(int); //正確,顯式的指定返回類型是一個指向函數(shù)的指針int (*f1(int))(int*, int);//當(dāng)然我們也能用更傻瓜的方式聲明這個函數(shù)4. 置尾返回類型
auto f1(int) -> int (*)(int*, int);5. 將decltype作用于某函數(shù)時,它返回函數(shù)類型非指針類型
string::size_type sumLength(const string&,const string&); decltype(sumLength) *getFcn(const string &);總結(jié)
以上是生活随笔為你收集整理的C++ Primer 5th笔记(6)chapter6 函数:函数指针的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ Primer 5th笔记(6)c
- 下一篇: C++ Primer 5th笔记(7)c