<<c++ primer>> 函数指针的笔记

本文介绍了C++中函数指针的基本概念及使用方法,包括如何定义和初始化函数指针,以及如何通过函数指针来调用函数。此外,还展示了函数指针作为参数传递给其他函数的例子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参见<<c++ primer>> p237页

bool (*pf)(const string &, const string &);
typedef bool (*cmpFcn) (const string &,const string &);//表示cmpFcn是一种指向函数的指针类型的名字
bool lengthCompare(const string &,const string &){return 0;}

int main()
{
	cmpFcn pf1 = 0;//函数指针初始化为0表示该指针不指向任何函数
	cmpFcn pf2 = lengthCompare;
	pf2 = pf1;

	pf1 = lengthCompare;
	pf2 = &lengthCompare;//在函数名上应用取地址操作符 等效于 直接引用函数名
	
	lengthCompare("hi","bye");
	
	(*pf2)("hi","bye");
	 pf1("hi","bye");//可以不需要使用解引用操作符,直接通过指针调用函数
	return 0;
}


void useBigger1(const string &,const string &,bool(const string&,const string&) );//函数指针作形参的两种形式
void useBigger2(const string &,const string &,bool (*)(const string&,const string&) );




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值