DECLARE_HANDLE 宏,#,## 预处理指令

在WINNT.H中有这样一段代码:

#define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#define sabc(val) #val
#define glue(a,b) a##b
 
#define MY_DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
 
struct HTYPE2__ {int unused;}; typedef struct HTYPE2__ * HTYPE2;
 
MY_DECLARE_HANDLE(HTYPE);
void CTestGetModuleHandleDlg::OnBtnSendmsg()
{  
    //MessageBox(sabc(中国));         //相当于 MessageBox("中国");
    /*
    int c=20,ab=0;
    glue(a,b) = ++c;    //相当于 ab = ++c;
    CString str;
    str.Format("%d",ab);
    MessageBox(str);
    */
     
    HTYPE__ type;
    type.unused = 10;
    HTYPE pType = &type;
    CString str;
 
    HTYPE2 pT = pType;  //error C2440: 'initializing' : cannot convert from 'struct HTYPE2__ *' to 'struct HTYPE__ *'
    str.Format("%d,%d",type.unused,pType->unused);
    MessageBox(str);
}

 

 

Function macro definitions accept two special operators (# and ##) in the replacement sequence:
If the operator # is used before a parameter is used in the replacement sequence, that parameter is replaced by a string literal (as if it were enclosed between double quotes)

1
#define str(x) #x cout << str(test);



This would be translated into:

 
cout << "test";



The operator ## concatenates two arguments leaving no blank spaces between them:

1

#define glue(a,b) a ## b glue(c,out) << "test";



This would also be translated into:

 
cout << "test";

 

现在明白了,之所以定义这个宏去声明 结构体与结构体指针,就是为了让句柄之间转化赋值时能在编绎时检测到不匹配的赋值。

从使用者的角度上看,也非常简练。不得不佩服微软的人,确实是个好点子。

 

 

posted @   庚武  Views(1564)  Comments(0Edit  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示