zoukankan      html  css  js  c++  java
  • DECLARE_HANDLE 宏,#,## 预处理指令

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

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

    #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";

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

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

  • 相关阅读:
    Python(一)-基础不牢,地动山摇
    微信公众号开启企业付款到用户
    CSS多行显示省略号
    angular service自定义返回
    CSS input radio和checkbox样式
    angular微信支付url未注册
    angular+ionic的兼容性问题
    js 字符串和16进制的互相转换
    js循环变量赋值
    CSS 两层实现垂直居中(外层固定宽高,内层文本不定行数)
  • 原文地址:https://www.cnblogs.com/wucg/p/2434328.html
Copyright © 2011-2022 走看看