클래스에서 함수 포인터 사용하기
출처가 정확히 어딘지는 모르겠지만 잘정리 되어 있어서 올려봅니다.
멤버 함수의 포인터는 포인터라 할 수 없습니다.
일종의 오프셋 개념으로 사용해서 포인터를 계산하게 됩니다.
static이 아닌 멤버함수는 __thiscall 규약을 따릅니다.
멤버 함수를 static 함수로 만들어주면 __cdecl 규약이 되어 잘 컴파일될겁니다.
보통 CALLBACK함수는 typedef 를 이용한 방법을 사용한다.
출처 : 내생각(일부) + 김경진님 + 염원영님
ex)
typedef int (*Fun) (int n);
class CTestThreadDlg : public CDialog
{
// Construction
public:
CTestThreadDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_TESTTHREAD_DIALOG };
Fun _Fun;
//생략
static int GetNum(int n);
};
int CTestThreadDlg::GetNum(int n)
{
return n;
}
BOOL CTestThreadDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//생략
//static으로 선언되어 있으므로 GetNum은 주소이다.
_Fun = GetNum;//방법1 typedef으로 선언한 변수를 사용하면 주소를 가질 수 있다.
int (*aaaa)(int aaaaa);//방법2 멤버변수가 아니므로 주소를 가질 수 있다.
aaaa = GetNum;
return TRUE; // return TRUE unless you set the focus to a control
}