首先我们先看类的定义;
- #include <iostream>
- using namespace std;
- class FunctionPoint
- {
- public:
- FunctionPoint(int s);
- ~FunctionPoint(void);
-
-
- static FunctionPoint* pFunctionPoint;
-
-
- static int FunctionName1(int a,int b);
- static int FunctionName2(int a,int b);
-
-
- int m_s;
- };
接下来我们可以查看类的实现:
- #include "FunctionPoint.h"
- FunctionPoint* FunctionPoint::pFunctionPoint=nullptr;
-
- FunctionPoint::FunctionPoint(int s)
- {
- pFunctionPoint =this;
- m_s = s;
- }
-
-
- FunctionPoint::~FunctionPoint(void)
- {
- }
-
- int FunctionPoint::FunctionName1(int a,int b)
- {
- cout<<a<<""<<b<<endl;
- cout<<pFunctionPoint->m_s<<endl;
- return 0;
- }
- int FunctionPoint::FunctionName2(int a,int b)
- {
- cout<<pFunctionPoint->m_s<<endl;
- cout<<b<<""<<a<<endl;
- return 0;
- }
最后我们实现如何使用:
- #include "FunctionPoint.h"
-
-
- int (*opp(FunctionPoint m_class,int FunctionIndex))(int a, int b)
- {
- if(FunctionIndex == 1)
- {
- return m_class.FunctionName1;
- }
- else
- {
- return m_class.FunctionName2;
- }
- }
-
- int main()
- {
- FunctionPoint fp1(2);
- printf("类的函数指针:%d, %d/n", opp(fp1,1)(3, 4), opp(fp1,2)(3, 4));
- FunctionPoint fp2(7);
- printf("类的函数指针:%d, %d/n", opp(fp1,1)(3, 4), opp(fp1,2)(3, 4));
- printf("类的函数指针:%d, %d/n", opp(fp2,1)(3, 4), opp(fp2,2)(3, 4));
- system("pause");
- return 0;
- }
|