函数指针定义
void func(string str){return;}
1.简单的函数指针的应用。
//形式1:返回类型(*函数名)(参数表)
void(*pFunc)(string);
用法:pFunc = func;
(*pFunc)("str");
2.使用typedef更直观更方便。
//形式2:typedef 返回类型(*新类型)(参数表)
typedef void(*pFunc)(string);
用法:pFunc pfunc;
pfunc = func;
(*pFunc)("str");
3.在C++类中使用函数指针。
//形式3:typedef 返回类型(类名::*新类型)(参数表)
class B
{
public:
void PrintStr(string val){
cout << "class B " << val << endl;
}
};
用法:
typedef void (B::*pFunc)(string);
pFunc ptrFunc;
ptrFunc = &B::PrintStr;
//1
B b;
(b.*ptrFunc)("b.*ptrFunc");
//2
B* pB = new B;
(pB->*ptrFunc)("new B->ptrFunc");
delete pB;
努力!从什么时候开始都不晚
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步