int copy(void) { cout << "copy" << endl; return 0; }int (* AFXCALL)(void)
AFXCALL=copy;(*AFXCALL)() 等价于 copy(),这里的AFXCALL是一个函数指针(返回值是int,输
入参数为void)
(int (*)(void))copy 等价于 AFXCALL(*(int (*)(void))copy)()就等价于 copy()了~,既然copy(在程序中,是唯一的),为何还要加(int (*)(void))copy ?
为什么在有些情况下不用加。下面这个代码: #include <iostream>
#include <string> using namespace std; // generic type of function pointer
typedef void (*PFUNC)(void);// command ID definition
#define CMD_COPY 0
#define CMD_PASTE 1
#define CMD_CUT 2
#define CMD_NULL 0xFFFF // command function implementation
int copy(void) { cout << "copy" << endl; return 0; }
bool paste(string& str) { cout << "paste: " << str << endl; return true; }
void cut(int i, int j) { cout << "cut: " << i << " " << j << endl; } enum Sig // ref <Dissecting MFC 2/e> p581
{
Sig_end = 0, // [s end of message map]
Sig_iv, // int (void)
Sig_bs, // bool (string&)
Sig_vii, // void (int, int)
}; union CmdFunctions // ref <Dissecting MFC 2/e> p583
{
PFUNC pfn; // generic function pointer // specific type safe variants
int (*pfn_iv)(void); //pfn_iv 是指向函数的指针
bool (*pfn_bs)(string&);
void (*pfn_vii)(int, int);
};struct Command // ref <Dissecting MFC 2/e> p580
{
unsigned int nCmd;
unsigned int nSig;
PFUNC pfn; // command handler
};
/*
Command commands[] = // ref <Dissecting MFC 2/e> p555
{
{ CMD_COPY, Sig_iv, (PFUNC)(int (*)(void))copy },
{ CMD_PASTE, Sig_bs, (PFUNC)(bool (*)(string&))paste },
{ CMD_CUT, Sig_vii, (PFUNC)(void (*)(int, int))cut },
{ CMD_NULL, Sig_end, (PFUNC)0 }
};
*/
Command commands[] = // ref <Dissecting MFC 2/e> p555
{
{ CMD_COPY, Sig_iv, (PFUNC)copy },
{ CMD_PASTE, Sig_bs, (PFUNC)paste },
{ CMD_CUT, Sig_vii, (PFUNC)cut },
{ CMD_NULL, Sig_end, (PFUNC)0 }
};
///////////////////////////////////////////////////////////////////////////
//这样就会提示copy不能转换,而其他的paste 和cut函数就可以转换,这个是为什么?
////////////////////////////////////////////////////////////////////////////// void main(void)
{
unsigned int icmd;
int iparam1, iparam2;
string sparam;
union CmdFunctions cf; // ref <Dissecting MFC 2/e> p581 while (1) // 模拟不断有 command 进来,不断地处理之
{ cout << "Command (0 or 1 or 2, any other will exit) : ";
cin >> icmd;
switch (icmd) {
case 0 :
break; case 1 :
cout << "param str = ";
cin >> sparam;
break; case 2 :
cout << "iparam1 = ";
cin >> iparam1;
cout << "iparam2 = ";
cin >> iparam2;
break; default :
cout << "command error!" << endl;
exit(1);
}
cf.pfn = commands[icmd].pfn; switch (commands[icmd].nSig)
{
case Sig_iv:
cout << "result= " << (*cf.pfn_iv)() << endl;
break; case Sig_bs:
cout << "result= " << (*cf.pfn_bs)(sparam) << endl;
break; case Sig_vii:
(*cf.pfn_vii)(iparam1, iparam2);
break;
}
} // while
}
AFXCALL=copy;(*AFXCALL)() 等价于 copy(),这里的AFXCALL是一个函数指针(返回值是int,输
入参数为void)
(int (*)(void))copy 等价于 AFXCALL(*(int (*)(void))copy)()就等价于 copy()了~,既然copy(在程序中,是唯一的),为何还要加(int (*)(void))copy ?
为什么在有些情况下不用加。下面这个代码: #include <iostream>
#include <string> using namespace std; // generic type of function pointer
typedef void (*PFUNC)(void);// command ID definition
#define CMD_COPY 0
#define CMD_PASTE 1
#define CMD_CUT 2
#define CMD_NULL 0xFFFF // command function implementation
int copy(void) { cout << "copy" << endl; return 0; }
bool paste(string& str) { cout << "paste: " << str << endl; return true; }
void cut(int i, int j) { cout << "cut: " << i << " " << j << endl; } enum Sig // ref <Dissecting MFC 2/e> p581
{
Sig_end = 0, // [s end of message map]
Sig_iv, // int (void)
Sig_bs, // bool (string&)
Sig_vii, // void (int, int)
}; union CmdFunctions // ref <Dissecting MFC 2/e> p583
{
PFUNC pfn; // generic function pointer // specific type safe variants
int (*pfn_iv)(void); //pfn_iv 是指向函数的指针
bool (*pfn_bs)(string&);
void (*pfn_vii)(int, int);
};struct Command // ref <Dissecting MFC 2/e> p580
{
unsigned int nCmd;
unsigned int nSig;
PFUNC pfn; // command handler
};
/*
Command commands[] = // ref <Dissecting MFC 2/e> p555
{
{ CMD_COPY, Sig_iv, (PFUNC)(int (*)(void))copy },
{ CMD_PASTE, Sig_bs, (PFUNC)(bool (*)(string&))paste },
{ CMD_CUT, Sig_vii, (PFUNC)(void (*)(int, int))cut },
{ CMD_NULL, Sig_end, (PFUNC)0 }
};
*/
Command commands[] = // ref <Dissecting MFC 2/e> p555
{
{ CMD_COPY, Sig_iv, (PFUNC)copy },
{ CMD_PASTE, Sig_bs, (PFUNC)paste },
{ CMD_CUT, Sig_vii, (PFUNC)cut },
{ CMD_NULL, Sig_end, (PFUNC)0 }
};
///////////////////////////////////////////////////////////////////////////
//这样就会提示copy不能转换,而其他的paste 和cut函数就可以转换,这个是为什么?
////////////////////////////////////////////////////////////////////////////// void main(void)
{
unsigned int icmd;
int iparam1, iparam2;
string sparam;
union CmdFunctions cf; // ref <Dissecting MFC 2/e> p581 while (1) // 模拟不断有 command 进来,不断地处理之
{ cout << "Command (0 or 1 or 2, any other will exit) : ";
cin >> icmd;
switch (icmd) {
case 0 :
break; case 1 :
cout << "param str = ";
cin >> sparam;
break; case 2 :
cout << "iparam1 = ";
cin >> iparam1;
cout << "iparam2 = ";
cin >> iparam2;
break; default :
cout << "command error!" << endl;
exit(1);
}
cf.pfn = commands[icmd].pfn; switch (commands[icmd].nSig)
{
case Sig_iv:
cout << "result= " << (*cf.pfn_iv)() << endl;
break; case Sig_bs:
cout << "result= " << (*cf.pfn_bs)(sparam) << endl;
break; case Sig_vii:
(*cf.pfn_vii)(iparam1, iparam2);
break;
}
} // while
}
但
#define ON_WM_CREATE() \
{ WM_CREATE, 0, 0, 0, AfxSig_is, \
(AFX_PMSG)(AFX_PMSGW)(int (AFX_MSG_CALL CWnd::*)(LPCREATESTRUCT))&OnCreate },疑问1:OnCreate 本来就 函数名,怎么还要加&
2: (AFX_PMSG)OnCreate 这样行不?
typedef void (AFX_MSG_CALL CWnd::*AFX_PMSGW)(void);
typedef void (AFX_MSG_CALL CCmdTarget::*AFX_PMSG)(void);过程:
1、(int (AFX_MSG_CALL CWnd::*)(LPCREATESTRUCT))&OnCreate 已经转换成 返回值为int ,参数为LPCREATESTRUCT
2、转换成AFX_PMSGW void的返回 void的参数
3、转换成AFX_PMSG void的返回 void的参数
为何那么麻烦?
虽然 系统的option-》derectory包含了<xutility>所含的路径
但程序并没有使用这个<xutility>,只使用了 #include <iostream>
#include <string>??
#include <iostream>
#include <string>
都连到(以下一层一层剥落)#include <istream>
#include <ostream>
#include <ios>
#include <streambuf>
#include <xlocnum>
#include <xiosbase>
#include <xlocale>
#include <stdexcept>
#include <xstring>#include <xutility>
#define ON_WM_CREATE() \
{ WM_CREATE, 0, 0, 0, AfxSig_is, \
(AFX_PMSG)(AFX_PMSGW)(int (AFX_MSG_CALL CWnd::*)(LPCREATESTRUCT))&OnCreate },疑问1:OnCreate 本来就 函数名,怎么还要加&
2: (AFX_PMSG)OnCreate 这样行不?
typedef void (AFX_MSG_CALL CWnd::*AFX_PMSGW)(void);
typedef void (AFX_MSG_CALL CCmdTarget::*AFX_PMSG)(void);过程:
1、(int (AFX_MSG_CALL CWnd::*)(LPCREATESTRUCT))&OnCreate 已经转换成 返回值为int ,参数为LPCREATESTRUCT
2、转换成AFX_PMSGW void的返回 void的参数
3、转换成AFX_PMSG void的返回 void的参数
为何那么麻烦?