青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

JACKY_ZZ[貓貓愛(ài)吃魚(yú)]

春風(fēng)拂面兩頰紅,秋葉灑地一片金。 夏荷搖曳一身輕,冬雪覆蓋大地銀。
posts - 30, comments - 123, trackbacks - 0, articles - 0

[C/C++] union初探

Posted on 2007-08-06 17:31 jacky_zz 閱讀(1300) 評(píng)論(4)  編輯 收藏 引用 所屬分類: C/C++
這幾天在看MFC深入淺出,看到一個(gè)union的定義,代碼如下:
  1union MessageMapFunctions
  2{
  3    AFX_PMSG pfn;   // generic member function pointer
  4
  5    BOOL (AFX_MSG_CALL CCmdTarget::*pfn_b_D)(CDC*);
  6    BOOL (AFX_MSG_CALL CCmdTarget::*pfn_b_b)(BOOL);
  7    BOOL (AFX_MSG_CALL CCmdTarget::*pfn_b_u)(UINT);
  8    BOOL (AFX_MSG_CALL CCmdTarget::*pfn_b_h)(HANDLE);
  9    BOOL (AFX_MSG_CALL CCmdTarget::*pfn_b_W_u_u)(CWnd*, UINT, UINT);
 10    BOOL (AFX_MSG_CALL CCmdTarget::*pfn_b_W_COPYDATASTRUCT)(CWnd*, COPYDATASTRUCT*);
 11    BOOL (AFX_MSG_CALL CCmdTarget::*pfn_b_HELPINFO)(LPHELPINFO);
 12    HBRUSH (AFX_MSG_CALL CCmdTarget::*pfn_B_D_W_u)(CDC*, CWnd*, UINT);
 13    HBRUSH (AFX_MSG_CALL CCmdTarget::*pfn_B_D_u)(CDC*, UINT);
 14    int (AFX_MSG_CALL CCmdTarget::*pfn_i_u_W_u)(UINT, CWnd*, UINT);
 15    int (AFX_MSG_CALL CCmdTarget::*pfn_i_u_u)(UINT, UINT);
 16    int (AFX_MSG_CALL CCmdTarget::*pfn_i_W_u_u)(CWnd*, UINT, UINT);
 17    int (AFX_MSG_CALL CWnd::*pfn_i_s)(LPTSTR);
 18    LRESULT (AFX_MSG_CALL CWnd::*pfn_l_w_l)(WPARAM, LPARAM);
 19    LRESULT (AFX_MSG_CALL CWnd::*pfn_l_u_u_M)(UINT, UINT, CMenu*);
 20    void (AFX_MSG_CALL CWnd::*pfn_v_v)();
 21    int (AFX_MSG_CALL CWnd::*pfn_i_u)(UINT);
 22    HCURSOR (AFX_MSG_CALL CWnd::*pfn_C_v)();
 23    UINT (AFX_MSG_CALL CWnd::*pfn_u_u)(UINT);
 24    BOOL (AFX_MSG_CALL CWnd::*pfn_b_v)();
 25    void (AFX_MSG_CALL CWnd::*pfn_v_u)(UINT);
 26    void (AFX_MSG_CALL CWnd::*pfn_v_u_u)(UINT, UINT);
 27    void (AFX_MSG_CALL CWnd::*pfn_v_i_i)(intint);
 28    void (AFX_MSG_CALL CWnd::*pfn_v_u_u_u)(UINT, UINT, UINT);
 29    void (AFX_MSG_CALL CWnd::*pfn_v_u_i_i)(UINT, intint);
 30    void (AFX_MSG_CALL CWnd::*pfn_v_w_l)(WPARAM, LPARAM);
 31    void (AFX_MSG_CALL CWnd::*pfn_v_b_W_W)(BOOL, CWnd*, CWnd*);
 32    void (AFX_MSG_CALL CWnd::*pfn_v_D)(CDC*);
 33    void (AFX_MSG_CALL CWnd::*pfn_v_M)(CMenu*);
 34    void (AFX_MSG_CALL CWnd::*pfn_v_M_u_b)(CMenu*, UINT, BOOL);
 35    void (AFX_MSG_CALL CWnd::*pfn_v_W)(CWnd*);
 36    void (AFX_MSG_CALL CWnd::*pfn_v_W_u_u)(CWnd*, UINT, UINT);
 37    void (AFX_MSG_CALL CWnd::*pfn_v_W_p)(CWnd*, CPoint);
 38    void (AFX_MSG_CALL CWnd::*pfn_v_W_h)(CWnd*, HANDLE);
 39    void (AFX_MSG_CALL CWnd::*pfn_v_u_W)(UINT, CWnd*);
 40    void (AFX_MSG_CALL CWnd::*pfn_v_u_W_b)(UINT, CWnd*, BOOL);
 41    void (AFX_MSG_CALL CWnd::*pfn_v_u_u_W)(UINT, UINT, CWnd*);
 42    void (AFX_MSG_CALL CWnd::*pfn_v_s)(LPTSTR);
 43    void (AFX_MSG_CALL CWnd::*pfn_v_u_cs)(UINT, LPCTSTR);
 44    void (AFX_MSG_CALL CWnd::*pfn_v_i_s)(int, LPTSTR);
 45    int (AFX_MSG_CALL CWnd::*pfn_i_i_s)(int, LPTSTR);
 46    UINT (AFX_MSG_CALL CWnd::*pfn_u_p)(CPoint);
 47    UINT (AFX_MSG_CALL CWnd::*pfn_u_v)();
 48    void (AFX_MSG_CALL CWnd::*pfn_v_b_NCCALCSIZEPARAMS)(BOOL, NCCALCSIZE_PARAMS*);
 49    void (AFX_MSG_CALL CWnd::*pfn_v_v_WINDOWPOS)(WINDOWPOS*);
 50    void (AFX_MSG_CALL CWnd::*pfn_v_u_u_M)(UINT, UINT, HMENU);
 51    void (AFX_MSG_CALL CWnd::*pfn_v_u_p)(UINT, CPoint);
 52    void (AFX_MSG_CALL CWnd::*pfn_v_u_pr)(UINT, LPRECT);
 53    BOOL (AFX_MSG_CALL CWnd::*pfn_b_u_s_p)(UINT, short, CPoint);
 54    LRESULT (AFX_MSG_CALL CWnd::*pfn_l_v)();
 55
 56    // type safe variant for thread messages
 57    void (AFX_MSG_CALL CWinThread::*pfn_THREAD)(WPARAM, LPARAM);
 58
 59    // specific type safe variants for WM_COMMAND and WM_NOTIFY messages
 60    void (AFX_MSG_CALL CCmdTarget::*pfnCmd_v_v)();
 61    BOOL (AFX_MSG_CALL CCmdTarget::*pfnCmd_b_v)();
 62    void (AFX_MSG_CALL CCmdTarget::*pfnCmd_v_u)(UINT);
 63    BOOL (AFX_MSG_CALL CCmdTarget::*pfnCmd_b_u)(UINT);
 64
 65    void (AFX_MSG_CALL CCmdTarget::*pfnNotify_v_NMHDR_pl)(NMHDR*, LRESULT*);
 66    BOOL (AFX_MSG_CALL CCmdTarget::*pfnNotify_b_NMHDR_pl)(NMHDR*, LRESULT*);
 67    void (AFX_MSG_CALL CCmdTarget::*pfnNotify_v_u_NMHDR_pl)(UINT, NMHDR*, LRESULT*);
 68    BOOL (AFX_MSG_CALL CCmdTarget::*pfnNotify_b_u_NMHDR_pl)(UINT, NMHDR*, LRESULT*);
 69    void (AFX_MSG_CALL CCmdTarget::*pfnCmdUI_v_C)(CCmdUI*);
 70    void (AFX_MSG_CALL CCmdTarget::*pfnCmdUI_v_C_u)(CCmdUI*, UINT);
 71
 72    void (AFX_MSG_CALL CCmdTarget::*pfnCmd_v_pv)(void*);
 73    BOOL (AFX_MSG_CALL CCmdTarget::*pfnCmd_b_pv)(void*);
 74
 75//OLD
 76    // specific type safe variants for WM-style messages
 77//    BOOL    (AFX_MSG_CALL CWnd::*pfn_bD)(CDC*);
 78//    BOOL    (AFX_MSG_CALL CWnd::*pfn_bb)(BOOL);
 79//    BOOL    (AFX_MSG_CALL CWnd::*pfn_bWww)(CWnd*, UINT, UINT);
 80//    BOOL    (AFX_MSG_CALL CWnd::*pfn_bHELPINFO)(HELPINFO*);
 81//    BOOL    (AFX_MSG_CALL CWnd::*pfn_bWCDS)(CWnd*, COPYDATASTRUCT*);
 82//    HBRUSH  (AFX_MSG_CALL CWnd::*pfn_hDWw)(CDC*, CWnd*, UINT);
 83//    HBRUSH  (AFX_MSG_CALL CWnd::*pfn_hDw)(CDC*, UINT);
 84//    int     (AFX_MSG_CALL CWnd::*pfn_iwWw)(UINT, CWnd*, UINT);
 85//    int     (AFX_MSG_CALL CWnd::*pfn_iww)(UINT, UINT);
 86//    int     (AFX_MSG_CALL CWnd::*pfn_iWww)(CWnd*, UINT, UINT);
 87//    int     (AFX_MSG_CALL CWnd::*pfn_is)(LPTSTR);
 88//    LRESULT (AFX_MSG_CALL CWnd::*pfn_lwl)(WPARAM, LPARAM);
 89//    LRESULT (AFX_MSG_CALL CWnd::*pfn_lwwM)(UINT, UINT, CMenu*);
 90//    void    (AFX_MSG_CALL CWnd::*pfn_vv)(void);
 91
 92//    void    (AFX_MSG_CALL CWnd::*pfn_vw)(UINT);
 93//    void    (AFX_MSG_CALL CWnd::*pfn_vww)(UINT, UINT);
 94//    void    (AFX_MSG_CALL CWnd::*pfn_vvii)(intint);
 95//    void    (AFX_MSG_CALL CWnd::*pfn_vwww)(UINT, UINT, UINT);
 96//    void    (AFX_MSG_CALL CWnd::*pfn_vwii)(UINT, intint);
 97//    void    (AFX_MSG_CALL CWnd::*pfn_vwl)(WPARAM, LPARAM);
 98//    void    (AFX_MSG_CALL CWnd::*pfn_vbWW)(BOOL, CWnd*, CWnd*);
 99//    void    (AFX_MSG_CALL CWnd::*pfn_vD)(CDC*);
100//    void    (AFX_MSG_CALL CWnd::*pfn_vM)(CMenu*);
101//    void    (AFX_MSG_CALL CWnd::*pfn_vMwb)(CMenu*, UINT, BOOL);
102
103//    void    (AFX_MSG_CALL CWnd::*pfn_vW)(CWnd*);
104//    void    (AFX_MSG_CALL CWnd::*pfn_vWww)(CWnd*, UINT, UINT);
105//    void    (AFX_MSG_CALL CWnd::*pfn_vWp)(CWnd*, CPoint);
106//    void    (AFX_MSG_CALL CWnd::*pfn_vWh)(CWnd*, HANDLE);
107//    void    (AFX_MSG_CALL CWnd::*pfn_vwW)(UINT, CWnd*);
108//    void    (AFX_MSG_CALL CWnd::*pfn_vwWb)(UINT, CWnd*, BOOL);
109//    void    (AFX_MSG_CALL CWnd::*pfn_vwwW)(UINT, UINT, CWnd*);
110//    void    (AFX_MSG_CALL CWnd::*pfn_vwwx)(UINT, UINT);
111//    void    (AFX_MSG_CALL CWnd::*pfn_vs)(LPTSTR);
112//    void    (AFX_MSG_CALL CWnd::*pfn_vOWNER)(int, LPTSTR);   // force return TRUE
113//    int     (AFX_MSG_CALL CWnd::*pfn_iis)(int, LPTSTR);
114//    UINT    (AFX_MSG_CALL CWnd::*pfn_wp)(CPoint);
115//    UINT    (AFX_MSG_CALL CWnd::*pfn_wv)(void);
116    void    (AFX_MSG_CALL CWnd::*pfn_vPOS)(WINDOWPOS*);
117    void    (AFX_MSG_CALL CWnd::*pfn_vCALC)(BOOL, NCCALCSIZE_PARAMS*);
118    void    (AFX_MSG_CALL CWnd::*pfn_vwp)(UINT, CPoint);
119    void    (AFX_MSG_CALL CWnd::*pfn_vwwh)(UINT, UINT, HANDLE);
120    BOOL    (AFX_MSG_CALL CWnd::*pfn_bwsp)(UINT, short, CPoint);
121//    void    (AFX_MSG_CALL CWnd::*pfn_vws)(UINT, LPCTSTR);
122};

而調(diào)用的方法只是指定了一個(gè)函數(shù)指針,而函數(shù)指針的定義為:
typedef void (AFX_MSG_CALL CCmdTarget::*AFX_PMSG)(void);
起初一點(diǎn)也不明白這樣的定義怎么指向?qū)嶋H執(zhí)行的函數(shù)?懷著這樣的疑問(wèn),我自己做了個(gè)測(cè)試,才明白其中的真實(shí)含義。union與struct的區(qū)別是:union為每個(gè)成員共享一個(gè)地址空間,而struct為每個(gè)成員都分配一個(gè)地址空間。這樣就好理解了,不管union里定義了多少個(gè)成員,每個(gè)成員都使用同一個(gè)內(nèi)存地址,而struct的每個(gè)成員的內(nèi)存地址卻都不相同。
測(cè)試代碼如下:

 1#include "stdafx.h"
 2
 3typedef void (*FUNC)(void);
 4
 5typedef void (*Func01)(intint);
 6typedef double (*Func02)(doubledouble);
 7typedef int (*Func03)(intint);
 8
 9void Test01(intint);
10double Test02(doubledouble);
11int Test03(intint);
12
13union UnionTest {
14    FUNC pFunc;
15
16    Func01 func01;
17    Func02 func02;
18    Func03 func03;
19};
20
21int _tmain(int argc, char* argv[])
22{
23    system("cls");
24    printf("此程序模仿了MFC中的消息函數(shù)分配機(jī)制\n");
25    union UnionTest test;
26
27    printf("(FUNC)(static_cast< void (*)(int, int) > (Test01))\n");
28    test.pFunc = (FUNC)(static_cast< void (*)(intint> (Test01));
29    printf("%p, %p, %p\n", test.func01, test.func02, test.func03);
30
31    test.func01(10,10);
32    test.func02(10.00120.002);
33    test.func03(10,10);
34    printf("\n");
35
36    printf("(FUNC)(static_cast< double (*)(double, double) > (Test02))\n");
37    test.pFunc = (FUNC)(static_cast< double (*)(doubledouble> (Test02));
38    printf("%p, %p, %p\n", test.func01, test.func02, test.func03);
39
40    test.func01(10,10);
41    double xx = test.func02(10.00120.002);
42    test.func03(10,10);
43    printf("\n");
44
45    printf("(FUNC)(static_cast< int (*)(int, int) > (Test03))\n");
46    test.pFunc = (FUNC)(static_cast< int (*)(intint> (Test03));
47    printf("%p, %p, %p\n", test.func01, test.func02, test.func03);
48
49    test.func01(10,10);
50    double y01 = test.func02(10.00120.002);
51    int yy = test.func03(10,10);
52
53    return 0;
54}
55
56void Test01(int x, int y) {
57    printf("hehe, test it. x=%d, y=%d\n", x, y);
58}
59
60double Test02(double x, double y) {
61    double sum =x+y;
62    printf("sum double: %10.10f+%10.10f=%10.10f\nx address:%p\ny address:%p\n",x,y,sum, &x, &y);
63    return sum;
64}
65
66int Test03(int x, int y) {
67    int sum =x+y;
68    printf("sum int: %d+%d=%d\n",x,y,sum);
69    return sum;
70}
輸出結(jié)果如下:
 1此程序模仿了MFC中的消息函數(shù)分配機(jī)制
 2(FUNC)(static_cast< void (*)(intint> (Test01))
 3004010000040100000401000
 4hehe, test it. x=10, y=10
 5hehe, test it. x=309237645, y=1076101251
 6hehe, test it. x=10, y=10
 7
 8(FUNC)(static_cast< double (*)(doubledouble> (Test02))
 9004010200040102000401020
10sum double0.0000000000+0.0000000000=0.0000000000
11x address:0012FEB0
12y address:0012FEB8
13sum double10.0010000000+20.0020000000=30.0030000000
14x address:0012FED8
15y address:0012FEE0
16sum double0.0000000000+10.0010000000=10.0010000000
17x address:0012FED0
18y address:0012FED8
19
20(FUNC)(static_cast< int (*)(intint> (Test03))
21004010700040107000401070
22sum int10+10=20
23sum int309237645+1076101251=1385338896
24sum int10+10=20
從輸出結(jié)果可以看出,union的每個(gè)成員的內(nèi)存地址都是一樣的。 這樣就好理解union MessageMapFunctions的真實(shí)作用了。

Feedback

# re: union初探  回復(fù)  更多評(píng)論   

2007-11-25 10:36 by aspmaster
不錯(cuò),雖然候Sir在深入淺出中說(shuō)了,但當(dāng)時(shí)還是沒(méi)有看明白

# re: union初探  回復(fù)  更多評(píng)論   

2012-07-09 10:34 by HooperStephanie21
The <a href="http://goodfinance-blog.com">loans</a> are useful for people, which would like to ground their own business. As a fact, this is comfortable to get a short term loan.

# re: union初探  回復(fù)  更多評(píng)論   

2013-03-25 14:10 by site
Look over EssaysLeader rewiew (best-essay-sites.com), select top-notch firm and purchase professionally written essays from competent writers.
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久精品国产一区二区三区| 亚洲国产成人午夜在线一区| 合欧美一区二区三区| 国产精品久久激情| 欧美日韩综合视频| 国产欧美va欧美va香蕉在| 国产一区二区高清不卡| 尤物九九久久国产精品的特点 | 久久一二三四| 你懂的网址国产 欧美| 欧美激情一区二区在线 | 亚洲一区在线免费| 久久九九精品99国产精品| 欧美91精品| 一本久道久久综合狠狠爱| 午夜精品一区二区三区在线| 久久av一区二区三区漫画| 欧美jizz19性欧美| 国产欧美高清| 99日韩精品| 男人的天堂成人在线| 在线亚洲国产精品网站| 欧美aaa级| 久久久久久久波多野高潮日日| 国产一级久久| 日韩写真视频在线观看| 久久精品免费播放| 日韩视频精品| 狂野欧美性猛交xxxx巴西| 国产精品免费一区二区三区观看| 在线观看一区| 久久久久久久999| 亚洲一区二区三区乱码aⅴ蜜桃女 亚洲一区二区三区乱码aⅴ | 久久亚洲国产精品日日av夜夜| 亚洲黄色一区二区三区| 欧美伊人久久久久久久久影院| 欧美三级网址| 亚洲精品一区二| 免费成人av资源网| 欧美一区二区三区在线播放| 欧美日韩国产综合新一区| 亚洲国产日韩在线| 美乳少妇欧美精品| 久久精品1区| 国产一区二区三区自拍| 欧美一区二区三区喷汁尤物| 日韩视频一区二区在线观看| 欧美jizz19hd性欧美| 亚洲第一精品久久忘忧草社区| 久久久久久一区二区三区| 先锋影音久久| 国产一区二区精品丝袜| 久久久999精品| 亚洲欧美日韩一区二区| 国产精品一区二区在线观看网站| 亚洲影视综合| 午夜精品久久久久久久久久久| 国产精品蜜臀在线观看| 午夜在线成人av| 午夜激情综合网| 国产亚洲精品一区二区| 久久久国产视频91| 久久久视频精品| 亚洲黄色免费网站| 亚洲精品自在久久| 国产精品大片| 久久国产精品72免费观看| 欧美中文字幕精品| 亚洲国产精品高清久久久| 亚洲缚视频在线观看| 欧美激情欧美狂野欧美精品 | 亚洲视频网站在线观看| 99在线精品视频在线观看| 国产精品人成在线观看免费 | 亚洲午夜一二三区视频| 亚洲伊人观看| 在线观看欧美日韩| 黄色影院成人| 亚洲性视频网站| 午夜精品久久久久久久久久久久 | 嫩草国产精品入口| 在线视频欧美日韩| 亚洲欧美在线观看| 亚洲国产精品va在线看黑人动漫| 亚洲日本国产| 国产欧美日韩视频一区二区| 欧美不卡视频一区| 国产精品久久国产三级国电话系列| 久久激情综合网| 免费日韩av片| 欧美一区二区三区精品| 美日韩在线观看| 亚洲一品av免费观看| 久久精品一区二区三区不卡牛牛 | 免费在线观看日韩欧美| 欧美日本高清| 蜜臀av在线播放一区二区三区| 欧美日韩不卡一区| 免费成人性网站| 国产精品天天看| 亚洲国产欧美精品| 国产综合激情| 亚洲天天影视| 一本色道**综合亚洲精品蜜桃冫| 欧美一级在线亚洲天堂| 亚洲一区二区三区在线观看视频| 老**午夜毛片一区二区三区| 亚久久调教视频| 欧美日韩一区二| 亚洲东热激情| 在线免费观看视频一区| 欧美一区二区三区喷汁尤物| 亚洲一区日韩| 欧美视频国产精品| 最新69国产成人精品视频免费| 国产亚洲成av人片在线观看桃| 亚洲老司机av| 一区二区三区精密机械公司| 模特精品裸拍一区| 欧美成人午夜激情| 韩国v欧美v日本v亚洲v| 午夜在线精品偷拍| 性色一区二区三区| 国产精品久久久久久妇女6080 | 美女网站在线免费欧美精品| 国产一区二区精品久久99| 亚洲小视频在线观看| 亚洲小视频在线| 欧美小视频在线| 亚洲视频1区2区| 午夜精品视频在线观看| 国产精品www网站| 亚洲欧美制服另类日韩| 午夜精品www| 欧美中文在线视频| 国产偷久久久精品专区| 欧美亚洲免费电影| 久久久水蜜桃| 玉米视频成人免费看| 久热精品在线| 最新成人在线| 中文国产成人精品久久一| 国产精品初高中精品久久| 亚洲新中文字幕| 欧美影院午夜播放| 禁久久精品乱码| 欧美国产一区视频在线观看 | 久久青草久久| 亚洲国产日韩一区| 中国女人久久久| 国产精品一区2区| 欧美在线视频导航| 欧美va天堂va视频va在线| 亚洲精品欧洲| 欧美日韩在线播放三区四区| 亚洲欧美乱综合| 欧美1区2区视频| 宅男噜噜噜66一区二区| 国产精品亚洲综合天堂夜夜| 久久久噜噜噜久噜久久| 亚洲理论在线| 久久久久久穴| 一本色道久久99精品综合 | 欧美激情2020午夜免费观看| 一区二区三区精密机械公司 | 亚洲高清中文字幕| 亚洲视频免费在线| 韩国成人福利片在线播放| 欧美激情网友自拍| 亚欧成人在线| 99在线热播精品免费99热| 久久亚洲欧美| 亚洲综合大片69999| 在线观看亚洲a| 国产精品视频导航| 欧美乱在线观看| 欧美夜福利tv在线| 99国产精品久久久久久久久久| 久热re这里精品视频在线6| 亚洲图片你懂的| 亚洲日本视频| 伊人成人在线| 国产亚洲成人一区| 欧美日韩黄视频| 老司机免费视频久久| 性欧美长视频| 一区二区三区欧美日韩| 欧美国产一区二区| 久久蜜桃av一区精品变态类天堂| 在线亚洲一区二区| 亚洲人成亚洲人成在线观看图片 | 午夜精品久久久久久久99热浪潮| 亚洲日本中文| 在线免费观看日本一区| 国产综合色一区二区三区| 国产精品视频一区二区高潮| 国产精品啊v在线| 欧美久久一区| 欧美成人综合网站| 免费观看成人www动漫视频|