类定义:CMemberFuncPointer
=======================================================================
1 // MemberFuncPointer.h: interface for the CMemberFuncPointer class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #if !defined(AFX_MEMBERFUNCPOINTER_H__4D78718D_778E_4427_BBC1_C5F2D52C64E5__INCLUDED_) 6 #define AFX_MEMBERFUNCPOINTER_H__4D78718D_778E_4427_BBC1_C5F2D52C64E5__INCLUDED_ 7 8 #if _MSC_VER > 1000 9 #pragma once 10 #endif // _MSC_VER > 1000 11 12 // 成员函数指针最大长度 13 #define MEMBER_FUNC_POINTER_MAX_SIZE 32 14 class CMemberFuncPointer 15 { 16 public: 17 void Reset(); 18 void Set(const void*pFunc,int p_size); 19 void Get(void*pFunc); 20 CMemberFuncPointer(); 21 CMemberFuncPointer(const char pointerdata[],int p_size); 22 CMemberFuncPointer(void* pointerdata,int p_size); 23 virtual ~CMemberFuncPointer(); 24 25 bool operator==(const CMemberFuncPointer &mfp) const; 26 bool operator!=(const CMemberFuncPointer &mfp) const; 27 28 CMemberFuncPointer& operator=(const CMemberFuncPointer &mfp); 29 30 private: 31 int m_size; 32 char m_pointer_data[MEMBER_FUNC_POINTER_MAX_SIZE]; 33 }; 34 35 #endif // !defined(AFX_MEMBERFUNCPOINTER_H__4D78718D_778E_4427_BBC1_C5F2D52C64E5__INCLUDED_)
MemberFuncPointer.h
1 // MemberFuncPointer.cpp: implementation of the CMemberFuncPointer class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #include "stdafx.h" 6 #include "OverlapRoutine.h" 7 #include "MemberFuncPointer.h" 8 9 #ifdef _DEBUG 10 #undef THIS_FILE 11 static char THIS_FILE[]=__FILE__; 12 #define new DEBUG_NEW 13 #endif 14 15 ////////////////////////////////////////////////////////////////////// 16 // Construction/Destruction 17 ////////////////////////////////////////////////////////////////////// 18 19 CMemberFuncPointer::CMemberFuncPointer() 20 { 21 this->m_size = 0; 22 } 23 24 CMemberFuncPointer::CMemberFuncPointer(const char pointerdata[],int p_size) 25 { 26 if(p_size<=MEMBER_FUNC_POINTER_MAX_SIZE) 27 { 28 this->m_size = p_size; 29 memcpy(this->m_pointer_data,pointerdata,p_size); 30 } 31 else 32 { 33 this->m_size = 0; 34 } 35 } 36 37 CMemberFuncPointer::CMemberFuncPointer(void* pointerdata,int p_size) 38 { 39 if(p_size<=MEMBER_FUNC_POINTER_MAX_SIZE) 40 { 41 this->m_size = p_size; 42 memcpy(this->m_pointer_data,pointerdata,p_size); 43 } 44 else 45 { 46 this->m_size = 0; 47 } 48 } 49 50 CMemberFuncPointer::~CMemberFuncPointer() 51 { 52 53 } 54 55 bool CMemberFuncPointer::operator==(const CMemberFuncPointer &mfp) const 56 { 57 if(this->m_size==mfp.m_size) //mfp.m_size 可以访问私有成员 58 { 59 if(0==memcmp(this->m_pointer_data,mfp.m_pointer_data,this->m_size)) 60 { 61 return true; 62 } 63 } 64 65 return false; 66 } 67 68 bool CMemberFuncPointer::operator!=(const CMemberFuncPointer &mfp) const 69 { 70 if(this->m_size==mfp.m_size) //mfp.m_size 可以访问私有成员 71 { 72 if(0==memcmp(this->m_pointer_data,mfp.m_pointer_data,this->m_size)) 73 { 74 return false; 75 } 76 } 77 78 return true; 79 } 80 81 void CMemberFuncPointer::Get(void *pFunc) 82 { 83 memcpy(pFunc,this->m_pointer_data,this->m_size); 84 } 85 86 void CMemberFuncPointer::Set(const void *pFunc,int p_size) 87 { 88 memcpy(this->m_pointer_data,pFunc,p_size); 89 this->m_size = p_size; 90 } 91 92 void CMemberFuncPointer::Reset() 93 { 94 this->m_size = 0; 95 } 96 97 CMemberFuncPointer& CMemberFuncPointer::operator =(const CMemberFuncPointer &mfp) 98 { 99 if(this != &mfp) 100 { 101 memcpy(this->m_pointer_data,mfp.m_pointer_data,mfp.m_size); //mfp.m_pointer_data 可以访问私有成员 102 this->m_size = mfp.m_size; //mfp.m_size 可以访问私有成员 103 } 104 105 return *this; 106 }
CMemberFuncPointer.cpp
时间: 2024-11-10 13:39:02