函式指標的兩個簡單用法
阿新 • • 發佈:2018-12-15
/************************************************************************* > File Name: func.cpp > Author: yangjx > Mail: [email protected] > Created Time: Sat 15 Dec 2018 12:28:19 PM CST ************************************************************************/ #include<stdio.h> #include<iostream> #include<vector> using namespace std; //one--------------------------------------------------- void myprint(int a) { printf("func==>%d\n", a); }; template<typename FUNC, typename V> void test(FUNC func, V v ) { func(v); } //two----------------------------------------------------- class A { public: A(); void Handle(); private: typedef void (*MYPRINT2)(int a); struct SPR { int v; MYPRINT2 p2; }; void reg(int v, MYPRINT2 p2); private: std::vector<SPR> _vecspr; }; void A::Handle() { for(std::vector<SPR>::iterator it = _vecspr.begin(); it != _vecspr.end(); ++it) { it->p2(it->v); } } void A::reg(int v, MYPRINT2 p2) { SPR spr; spr.v = v; spr.p2 = p2; _vecspr.push_back(spr); } void myprint2(int a) { printf("func2==>%d\n", a); } void myprint3(int a) { printf("func3==>%d\n", a); } A::A() { reg(1000, myprint2); reg(1003, myprint3); } int main() { test(myprint, 10); //-- A a; a.Handle(); return 0; }