#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void addNum( int a,int b){ printf("%d+%d=%d\r\n", a,b,a+b ); }
int addNum2( int a,int b){ return a+b; }
double addNum3( double a,double b){ return a- b; }
template<typename RT,typename argType>
class MATH
{
public:
MATH():personObj(){}
void setFpointer( RT (*funP)(argType,argType) );
union
{
RT (*funP)(argType,argType);
void* vPointF;
}_pOPF;
struct person
{ person(){ printf("person()\r\n"); strcpy(myName,"CYH"); age=9999; }
char myName[8];
int age;
}personObj;
};
template<typename RT,typename argType>
void MATH<RT,argType>::setFpointer( RT (*funP)(argType,argType) )
{
_pOPF.funP = funP;
}
int main(int argc, char** argv) {
MATH<void,int> MATHObj;
MATH<int,int> intMATHObj;
MATH<double,double> doubleMATHObj;
MATHObj.setFpointer( addNum );
MATHObj._pOPF.funP( 100,10 );
intMATHObj.setFpointer( addNum2 );
printf( "intMATHObj._pOPF.funP(200,20)=%d\r\n",intMATHObj._pOPF.funP(200,20) );
doubleMATHObj.setFpointer( addNum3 );
printf( "doubleMATHObj._pOPF.funP(200.2,20.2)=%f\r\n",doubleMATHObj._pOPF.funP(200.2,20.2) );
printf( "%s age is %d\r\n", MATHObj.personObj.myName, MATHObj.personObj.age );
system("PAUSE");
return 0;
}
沒有留言:
張貼留言