//比较两个string对象的长度
bool lengthCompare(const string& s1,const string& s2);bool(const string&,const string&). //pf指向一个函数,该函数的两个参数是const string的引用,返回值是bool类型
bool (*pf)(const string & , const string &);//未初始化注意:pf两端的括号比不可少,如果不写,则pf是一个返回类型为bool指针的函数
pf = lengthCompare;//pf指向名为lengthCompare的函数
pf=&lengthCompare; //等价赋值语句:取地址符是可逆的 bool b1 = pf("hello", "goodbye");//调用lengthCompare函数
bool b2 = (*pf)("hello", "goodbye");//一个等价的调用
bool b3 = lengthCompare("hello", "goodbye");//另一个等价调用bool lengthCompare(const string& s1, const string& s2)
{
cout <<s1<<" "<<s2<< endl;
return true;
}
string::size_type sumLength(const string&, const string&)
{
return 0;
}
bool cstringCompare(const char*, const char*)
{
return true;
} pf = 0;//正确:pf不指向任何函数
pf = sumLength;//错误:返回类型不匹配
pf = cstringCompare;//错误:形参类型不匹配
pf = lengthCompare;//正确:函数和指针类型精确匹配void ff(int*)
{
cout << "ff(int*)" << endl;
}
void ff(unsigned int)
{
cout << "ff unsigned int" << endl;
}
void test()
{
void (*pf1)(unsigned int) = ff;//pf1指向ff(unsigned)
pf1(1);
}
void (*pf2)(int) = ff;//错误:没有任何一个ff与该形参列表匹配
int (*pt3)(int*) = ff;//错误:没有任何一个ff与pt3的返回类型匹配//比较两个string对象的长度
bool lengthCompare(const string& s1, const string& s2)
{
if(s1.size()>s2.size())
return true;
return false;
}
//第三个参数是函数类型,它会自动转换成指向函数的指针
bool useBigger(const string& s1, const string& s2, bool bf(const string& s1, const string& s2))
{
return bf(s1, s2);
}
void test()
{
string s1 = "abc";
string s2 = "abcdfe";
if (useBigger(s1, s2, lengthCompare))
{
cout << "s1大于s2" << endl;
}
else
cout << "s1小于s2" << endl;
}//等价的声明:显示地将形参定义成指向函数的指针
bool useBigger(const string& s1, const string& s2, bool (*bf)(const string& s1, const string& s2))
{
return bf(s1, s2);
}//自动将函数lengthCompare转换成指向该函数的指针
useBigger(s1, s2, lengthCompare);
//或者也可以用取地址符
useBigger(s1, s2, &lengthCompare);
//func和func2是函数类型
typedef bool func(const string&, const string&);
typedef decltype(lengthCompare) func2;//等价的类型
//funcp和funcp2是指向函数的指针
typedef bool(*funcp)(const string&, const string&);
typedef decltype(lengthCompare)* funcp2;//等价的类型 //useBigger的等价声明,其中使用了类型别名
void useBigger(const string & s1, const string & s2, func);
void useBigger(const string & s1, const string & s2, func2);
void useBigger(const string & s1, const string & s2, funcp);
void useBigger(const string & s1, const string & s2, funcp2);using F = int(int*, int*);//F是函数类型,不是指针
using FF = int(*)(int*, int*);//FF是指针类型注意:返回值不会自动地转换为指针,我们必须显示将返回类型指定为指针
FF f1(int);//正确:FF是指向函数的指针,f1返回指向函数的指针
F f2(int);//错误:F是函数类型,f1不能返回一个函数
F* f3(int);//正确:显示地指定返回类型是指向函数的指针 int (*f1(int))(int*, int*);int addSum(int* a, int* b)
{
cout << "a+b= " << *a + *b << endl;
return 1;
}
int (*f1(int val))(int*, int*)
{
addSum(&val, &val);
return addSum;
}
void test()
{
auto ret = f1(10);
}
string::size_type a(const string& s1, const string& s2)
{
return s1.size() + s1.size();
}
string::size_type b(const string& s1)
{
return s1.size() + s1.size();
}
decltype(a) * getFunc(const string& s)
{
cout << "a(s,s)= " << a(s, s) << endl;
b(s);
return a;
}
void test()
{
auto ret = getFunc("abc");
cout << ret("abcd","casd") << endl;
//直接打印函数名,或解引用的效果一样,得到的都是函数的地址
cout << ret<< endl;
cout << *ret<< endl;
}

点个赞,再走吧,个位亲