• <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>

            公告

            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            統(tǒng)計(jì)

            • 隨筆 - 9
            • 文章 - 13
            • 評(píng)論 - 3
            • 引用 - 0

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            2007年10月23日

            使用標(biāo)準(zhǔn)C++的類型轉(zhuǎn)換符:static_cast、dynamic_cast、reinterdivt_cast、和const_cast

            使用標(biāo)準(zhǔn)C++的類型轉(zhuǎn)換符:static_cast、dynamic_cast、reinterdivt_cast、和const_cast。

            3.1 static_cast
            用法:static_cast < type-id > ( exdivssion )
            該運(yùn)算符把exdivssion轉(zhuǎn)換為type-id類型,但沒有運(yùn)行時(shí)類型檢查來保證轉(zhuǎn)換的安全性。它主要有如下幾種用法:
            ①用于類層次結(jié)構(gòu)中基類和子類之間指針或引用的轉(zhuǎn)換。
              進(jìn)行上行轉(zhuǎn)換(把子類的指針或引用轉(zhuǎn)換成基類表示)是安全的;
              進(jìn)行下行轉(zhuǎn)換(把基類指針或引用轉(zhuǎn)換成子類表示)時(shí),由于沒有動(dòng)態(tài)類型檢查,所以是不安全的。
            ②用于基本數(shù)據(jù)類型之間的轉(zhuǎn)換,如把int轉(zhuǎn)換成char,把int轉(zhuǎn)換成enum。這種轉(zhuǎn)換的安全性也要開發(fā)人員來保證。
            ③把空指針轉(zhuǎn)換成目標(biāo)類型的空指針。
            ④把任何類型的表達(dá)式轉(zhuǎn)換成void類型。

            注意:static_cast不能轉(zhuǎn)換掉exdivssion的const、volitale、或者_(dá)_unaligned屬性。


            3.2 dynamic_cast
            用法:dynamic_cast < type-id > ( exdivssion )
            該運(yùn)算符把exdivssion轉(zhuǎn)換成type-id類型的對(duì)象。Type-id必須是類的指針、類的引用或者void *;
            如果type-id是類指針類型,那么exdivssion也必須是一個(gè)指針,如果type-id是一個(gè)引用,那么exdivssion也必須是一個(gè)引用。

            dynamic_cast主要用于類層次間的上行轉(zhuǎn)換和下行轉(zhuǎn)換,還可以用于類之間的交叉轉(zhuǎn)換。
            在類層次間進(jìn)行上行轉(zhuǎn)換時(shí),dynamic_cast和static_cast的效果是一樣的;
            在進(jìn)行下行轉(zhuǎn)換時(shí),dynamic_cast具有類型檢查的功能,比static_cast更安全。
            class B{
            public:
            int m_iNum;
            virtual void foo();
            };

            class D:public B{
            public:
            char *m_szName[100];
            };

            void func(B *pb){
            D *pd1 = static_cast(pb);
            D *pd2 = dynamic_cast(pb);
            }

            在上面的代碼段中,如果pb指向一個(gè)D類型的對(duì)象,pd1和pd2是一樣的,并且對(duì)這兩個(gè)指針執(zhí)行D類型的任何操作都是安全的;
            但是,如果pb指向的是一個(gè)B類型的對(duì)象,那么pd1將是一個(gè)指向該對(duì)象的指針,對(duì)它進(jìn)行D類型的操作將是不安全的(如訪問m_szName),
            而pd2將是一個(gè)空指針。

            另外要注意:B要有虛函數(shù),否則會(huì)編譯出錯(cuò);static_cast則沒有這個(gè)限制。
            這是由于運(yùn)行時(shí)類型檢查需要運(yùn)行時(shí)類型信息,而這個(gè)信息存儲(chǔ)在類的虛函數(shù)表(
            關(guān)于虛函數(shù)表的概念,詳細(xì)可見)中,只有定義了虛函數(shù)的類才有虛函數(shù)表,
            沒有定義虛函數(shù)的類是沒有虛函數(shù)表的。

            另外,dynamic_cast還支持交叉轉(zhuǎn)換(cross cast)。如下代碼所示。
            class A{
            public:
            int m_iNum;
            virtual void f(){}
            };

            class B:public A{
            };

            class D:public A{
            };

            void foo(){
            B *pb = new B;
            pb->m_iNum = 100;

            D *pd1 = static_cast(pb); //compile error
            D *pd2 = dynamic_cast(pb); //pd2 is NULL
            delete pb;
            }

            在函數(shù)foo中,使用static_cast進(jìn)行轉(zhuǎn)換是不被允許的,將在編譯時(shí)出錯(cuò);而使用 dynamic_cast的轉(zhuǎn)換則是允許的,結(jié)果是空指針。


            3.3 reindivter_cast
            用法:reindivter_cast (exdivssion)
            type-id必須是一個(gè)指針、引用、算術(shù)類型、函數(shù)指針或者成員指針。
            它可以把一個(gè)指針轉(zhuǎn)換成一個(gè)整數(shù),也可以把一個(gè)整數(shù)轉(zhuǎn)換成一個(gè)指針(先把一個(gè)指針轉(zhuǎn)換成一個(gè)整數(shù),
            在把該整數(shù)轉(zhuǎn)換成原類型的指針,還可以得到原先的指針值)。

            該運(yùn)算符的用法比較多。

            3.4 const_cast
            用法:const_cast (exdivssion)
            該運(yùn)算符用來修改類型的const或volatile屬性。除了const 或volatile修飾之外, type_id和exdivssion的類型是一樣的。
            常量指針被轉(zhuǎn)化成非常量指針,并且仍然指向原來的對(duì)象;
            常量引用被轉(zhuǎn)換成非常量引用,并且仍然指向原來的對(duì)象;常量對(duì)象被轉(zhuǎn)換成非常量對(duì)象。

            Voiatile和const類試。舉如下一例:
            class B{
            public:
            int m_iNum;
            }
            void foo(){
            const B b1;
            b1.m_iNum = 100; //comile error
            B b2 = const_cast(b1);
            b2. m_iNum = 200; //fine
            }
            上面的代碼編譯時(shí)會(huì)報(bào)錯(cuò),因?yàn)閎1是一個(gè)常量對(duì)象,不能對(duì)它進(jìn)行改變;
            使用const_cast把它轉(zhuǎn)換成一個(gè)常量對(duì)象,就可以對(duì)它的數(shù)據(jù)成員任意改變。注意:b1和b2是兩個(gè)不同的對(duì)象。
             

            == ===========================================

            == dynamic_cast .vs. static_cast
            == ===========================================

            class B { ... };
            class D : public B { ... };

            void f(B* pb)
            {

            D* pd1 = dynamic_cast(pb);

            D* pd2 = static_cast(pb);
            }

            If pb really points to an object of type D, then pd1 and pd2 will get the same value. They will also get the same value if pb == 0.

            If pb points to an object of type B and not to the complete D class, then dynamic_cast will know enough to return zero. However, static_cast relies on the programmer’s assertion that pb points to an object of type D and simply returns a pointer to that supposed D object.

            dynamic_cast可用于繼承體系中的向下轉(zhuǎn)型,即將基類指針轉(zhuǎn)換為派生類指針,比static_cast更嚴(yán)格更安全。dynamic_cast在執(zhí)行效率上比static_cast要差一些,但static_cast在更寬上范圍內(nèi)可以完成映射,這種不加限制的映射伴隨著不安全性。static_cast覆蓋的變換類型除類層次的靜態(tài)導(dǎo)航以外,還包括無映射變換、窄化變換(這種變換會(huì)導(dǎo)致對(duì)象切片,丟失信息)、用VOID*的強(qiáng)制變換、隱式類型變換等...


            == ===========================================
            == static_cast .vs. reinterdivt_cast
            == ================================================

            reinterdivt_cast是為了映射到一個(gè)完全不同類型的意思,這個(gè)關(guān)鍵詞在我們需要把類型映射回原有類型時(shí)用到它。我們映射到的類型僅僅是為了故弄玄虛和其他目的,這是所有映射中最危險(xiǎn)的。(這句話是C++編程思想中的原話)

            static_cast reinterdivt_cast 操作符修改了操作數(shù)類型。它們不是互逆的; static_cast 在編譯時(shí)使用類型信息執(zhí)行轉(zhuǎn)換,在轉(zhuǎn)換執(zhí)行必要的檢測(cè)(諸如指針越界計(jì)算, 類型檢查). 其操作數(shù)相對(duì)是安全的。另一方面;reinterdivt_cast 僅僅是重新解釋了給出的對(duì)象的比特模型而沒有進(jìn)行二進(jìn)制轉(zhuǎn)換, 例子如下:

            int n=9; double d=static_cast < double > (n);

            上面的例子中, 我們將一個(gè)變量從 int 轉(zhuǎn)換到 double 這些類型的二進(jìn)制表達(dá)式是不同的。 要將整數(shù) 9 轉(zhuǎn)換到 雙精度整數(shù) 9static_cast 需要正確地為雙精度整數(shù) d 補(bǔ)足比特位。其結(jié)果為 9.0。而reinterdivt_cast 的行為卻不同:

            int n=9;

            double d=reinterdivt_cast (n);

            這次, 結(jié)果有所不同. 在進(jìn)行計(jì)算以后, d 包含無用值. 這是因?yàn)?/span> reinterdivt_cast 僅僅是復(fù)制 n 的比特位到 d, 沒有進(jìn)行必要的分析.

            因此, 你需要謹(jǐn)慎使用 reinterdivt_cast.

            posted @ 2007-10-23 20:28 blues 閱讀(3179) | 評(píng)論 (0)編輯 收藏

            2007年10月22日

            offsetof 解析

             1offsetof(s,m)解析 offsetof(s,m)解析
             2 
             3今天看代碼時(shí),發(fā)現(xiàn)一個(gè)有用的東東,offsetof(s,m),這是一個(gè)宏,定義如下:
             4
             5 #define offsetof(s,m) (size_t)&(((s *)0)->m)
             6 
             7 然后到網(wǎng)上查了一下,發(fā)現(xiàn)還真的是很有用,附帶一位大俠的解說:
             8
             9  struct   AAA   
            10  {   
            11  int   i;   
            12  int   j;   
            13  }
            ;   
            14    
            15  struct AAA *pAAA;   
            16  pAAA=new AAA;   
            17  這時(shí),pAAA實(shí)際上是一個(gè)Pointer, 指向某一確定的內(nèi)存地址,比如0x1234;   
            18  而 pAAA->i 整體是一個(gè)int型變量,其地址是&(pAAA->i) ,'&'為取址運(yùn)算符;   
            19  那么&(pAAA->i)一定等于0x1234,因?yàn)閕是結(jié)構(gòu)體AAA的第一個(gè)元素。   
            20  而&(pAAA->j)一定是0x1234 + 0x4 = 0x1238; 因?yàn)閟izeof(int= 4;
            21    
            22  這個(gè)做法的巧妙之處就是:它把“0”作為上例中的pAAA,那么 &(pAAA->j)就是j的   
            23  offset啦
            24
            25  解析結(jié)果是:   
            26  (s   *)0 ,將 0 強(qiáng)制轉(zhuǎn)換為Pointer to   "s"     
            27  可以記 pS = (s *)0 ,pS是指向s的指針,它的值是0;   
            28  那么pS->m就是m這個(gè)元素了,而&(pS->m)就是m的地址,而在本例中就是offset啦   
            29    
            30  再把結(jié)果強(qiáng)制轉(zhuǎn)換為size_t型的就OK 了,size_t其實(shí)也就是int啦!!    
            31 
            32 
            33

            posted @ 2007-10-22 16:02 blues 閱讀(4213) | 評(píng)論 (2)編輯 收藏

            2007年1月26日

            2006年世界頂級(jí)殺毒軟件排名

            http://blog.cnetnews.com.cn/hand/keji/3658/

            posted @ 2007-01-26 14:55 blues 閱讀(285) | 評(píng)論 (0)編輯 收藏

            2006年12月11日

            com中以結(jié)構(gòu)體作為接口參數(shù)

            ?1 通過VARIANT;???
            ?2 ??VARIANT???varData;(出參)???
            ?3 ????
            ?4 ??MYSTRUCT??? * pMyData??? = ???NULL;???
            ?5 ????
            ?6 ??pMyData??? = ???(MYSTRUCT * )CoTaskMemAlloc( sizeof (MYSTRUCT));???
            ?7 ??.???
            ?8 ????
            ?9 ??varData.byref??? = ???(LPVOID)pMyData;???
            10 ????
            11 ??在調(diào)用文件里,同樣定義???
            12 ??VARIANT???varData(入?yún)ⅲ???
            13 ????
            14 ??MYSTRUCT??? * pMyData??? = ???NULL;???
            15 ????
            16 ??pMyData??? = ???(MYSTRUCT * )varData.byref;???
            17 ????
            18 ??CoTaskMemFree((LPVOID)pMyData);

            posted @ 2006-12-11 15:39 blues 閱讀(977) | 評(píng)論 (0)編輯 收藏

            2006年11月7日

            Getting an (ATL) ActiveX control to print from Office Applications

            13 votes for this article.
            Popularity: 4.73. Rating: 4.25 out of 5.

            Introduction

            Seeing as this is my first ever post to CodeProject, let me do a quick introduction as to who I am and what I do. I have been working in one form of C and C++ or another for as long as I can remember (among the other myriad of languages that I've run into). Nowadays, most of my development is focused on Microsoft Windows platforms, and is done in VC6, VC2002.NET. I am heavily entrenched in BI (Business Intelligence) development, and in my spare time develop little ActiveX controls and games etc...

            The plot

            Having learnt COM a while ago, I made the obvious progression to ATL to ease the development of boilerplate code, and to leverage off Microsoft's template library. As my experience grew, I ventured into creating ActiveX controls using the ATL framework ... and life was good. I could spew out a fairly useful (albeit not overly complex) control within a short period of time. Recently, I was asked to create a KPI (Key Performance Indicator) control that could be embedded in a web page and an Excel document. Obviously based on my experience (which was obviously not vast) I thought that this would be no problem and off I went, creating code that would meet the functional spec (we all work to these don't we :)).

            A couple of days later the control was finished and the final tests were being run when someone asked me to print a hardcopy of an example spreadsheet with the embedded control. This is where my nightmares began. Not only did my control not print, but there was no clear indication as to why it didn't print. And so my exploration into this apparent mystery began.

            Have you ever tried to include 3rd party ActiveX controls into an Office document? They sure seem to work fine, but most (apart from the Microsoft controls) don't seem to render themselves when you request a Print Preview or a simple Print of the worksheet or document. So, if any of you have ever had this problem, or have never dabbled with this, but think that you may be heading this way, take note of this, cos it might save you hours of frustration and frantic searching on MSDN and Google.

            So what now?

            The first thing one needs to realize is that even though we have been blessed with Office 2000 and Office XP, the printing architecture still uses the old Windows-format metafile for its printing operations. This metafile format was used in 16-bit Windows-based applications (thinks back to Win3.1). Now, this becomes a major problem for ActiveX developers who wish their controls to be printable from within Office applications, because this old metafile format only supported a limited set of GDI functionality. The list of supported GDI functions can be found here.

            Now that you are armed with your limited function set, you cringe with the realization that you can no longer create memory DC's, you can no longer use your lovely DrawText() functions and you can definitely no longer call GetTextExtentPoint32() function. However, those realizations only hold true for the instance of when your control is being rendered to an old format metafile. So how do we empower our control to know that its being rendered to an old format metafile? Simple, we use the GetObjectType() function and check if the result is equal to OBJ_METADC (old metafile format):

            HRESULT Cxxxxx::OnDraw(ATL_DRAWINFO& di)
            {
                HDC hdc = di.hdcDraw;
                bool bMetaFile = false;
            
                //// lets check if we're drawing to an old// metafile format.. (like Office printing)//if ( GetObjectType(hdc) == OBJ_METADC )
                    bOldMetaFile = true;
            
                //// the rest of your code...//
            }

            For interest, the opposite of OBJ_METADC is OBJ_ENHMETADC (refer to this MSDN document).

            Now that we know if we're drawing to an old metafile format or not, we can write adaptive code to cater for each instance or we can just write all our drawing logic using the limited set of functionality that is supported by the old metafile DC.

            What about fonts and text extents?

            As any ATL ActiveX developer knows, using fonts in AX controls provides for limited amount of fun. The typical piece of code would probably look something like this:

            																//
            																// ... some code
            																//
                CComQIPtr<IFont, &IID_IFont> pFont(m_pFont);
                TEXTMETRICOLE tm;if ( pFont != NULL )
                {
                    pFont->get_hFont(&newFont);
                    pFont->AddRefHfont(newFont);
                    pFont->QueryTextMetrics(&tm);
                    oldFont = (HFONT) SelectObject(dc, newFont);
                }

            The Bolded lines of code are ones that I didn't use regularly, due to the fact that I didn't really need to know about the breakdown of my font's details because I had access to GetTextExtentPoint32() function. Unfortunately, in this scenario, we don't have access to that function to determine how wide (in pixels) our text is going to be. But there is another way to calculate this fairly accurately, as is demonstrated in the code below:

            																//
            																// assume that we have called QueryTextMetrics() and
            																// have a filled TEXTMETRICOLE structure called tm
            																//
            CComBSTR strText(_T("Hello, world"));
            SIZE sz;
            
            sz.cx = strText.Length() * tm.tmAveCharWidth;
            sz.cy = tm.tmHeight;

            Having said this, there are many other functions that I use a lot that I can't use if I want my ActiveX control to be printable by Office, but as with GetTextExtentPoint32() and its respective replacement, there is always a way to replace these functions using Old-Metafile-Safe-Drawing-Code (OMSDC). *maybe that acronym will catch on*

            Conclusion

            When creating an ActiveX control that you know will be used inside Office applications, and will most probably be printed, remember to stick to these guidelines when developing your drawing logic. I was fairly shocked by how little information was available in the MSDN and online in general, while I was searching for information on how to enable my ActiveX control to print from within an Office application. There are hundreds of documents on ActiveX controls being printed from within Internet Explorer, but none address this particular issue. Perhaps I was looking in the wrong places. Hopefully this article will help one or more of you one day ;)

            Acknowledgment

            Many thanks to Igor Tandetnik for pointing me in the right direction on this.

            About Peter Mares

            posted @ 2006-11-07 17:38 blues 閱讀(524) | 評(píng)論 (0)編輯 收藏
            轉(zhuǎn)===如何用ATL創(chuàng)建ActiveX控件(牛人翻譯的)

            http://www.czvc.com/down.asp?id=105

            posted @ 2006-11-07 16:53 blues 閱讀(516) | 評(píng)論 (0)編輯 收藏

            2006年9月12日

            DB2免費(fèi)版

            http://www.ibm.com/developerworks/cn/downloads/im/udbexp/

            posted @ 2006-09-12 14:08 blues 閱讀(418) | 評(píng)論 (0)編輯 收藏

            2006年8月25日

            美國(guó)讓人噴飯的法律

            ?

            這美國(guó)還有這等噴飯的法律,實(shí)屬大開眼界,看來有機(jī)會(huì)要好好善加利用了!!現(xiàn)與各位一起分享:?
            美國(guó)聯(lián)邦法律規(guī)定:?

            1)不得與豪豬發(fā)生性關(guān)系。(*,誰敢呀)?
            2)每周四晚6:00以后不得放P。(以后還真要小心了,別一不留神坐牢了還不知為?
            啥)?
            3)任何人不得銷售其子女。(好象中國(guó)也不許吧)?

            阿拉巴馬州:?
            無論任何時(shí)候,將冰激淋卷放在口袋里是違法的。(有病丫的)?

            阿肯色州:?
            男性可以合法毆打其配偶,但每月最多一次。(估計(jì)很多東北的兄弟知道了一定想?
            移民阿肯色了,可也有例外呀,克林頓就是阿肯色的前州長(zhǎng),咋老被喜萊莉扁呀)?

            亞利桑納州:?
            任何房間中不得有兩根以上的假****。(估計(jì)那州的最高法官丫是個(gè)變態(tài)狂!)?

            夏威夷州:?
            不得將谷物放在耳朵里。(神經(jīng)病,以為偷太空種子呀)?

            印弟安納州:?
            1)任何年滿18歲的男性,若與17歲以下的女性發(fā)生性關(guān)系,而且當(dāng)時(shí)她又沒穿鞋?
            襪,那將課重罪。(兄弟們千萬注意了呀!別全脫了)?
            2)圓周率在該州法定為4。(活活氣死咱祖沖之前輩呀!)?

            愛荷華州:?
            1)任何只有一只上臂的鋼琴演奏者必須免費(fèi)演奏。(嚴(yán)重歧視殘疾藝術(shù)表演家)?
            2)任何有胃病的男性不得在公共場(chǎng)所與女性接吻。(接吻和胃有關(guān)系嗎?男性胃癌?
            晚期患者的福音)?

            紐約州:?
            1)不得僅為娛樂而將球砸向他人腦袋。(謀殺可以不?真的腦子進(jìn)水了)?
            2)10:00以后不得穿拖鞋。(光腳吧)?

            新澤西州:?
            凡謀殺時(shí)不得穿防彈背心。(管得著嗎,警察這么沒自信!)?

            北卡州:?
            任何一位未婚男性與一為未婚女性,如果在任何旅館或汽車旅館登記為已婚,那么?
            他們即算合法夫妻了。(想帶小蜜開房的兄弟們千萬別去那州呀!)?

            賓西法尼亞州:?
            不得在浴室唱歌。(難怪在賓大商學(xué)院的同胞都不會(huì)K歌)?

            南卡州:?
            僅在每周六,男性被允許在法院的門前臺(tái)階上合法毆打其配偶。(這是啥規(guī)定,郁?
            悶ING)?

            猶他州:?
            1)不喝牛奶違法。(喝不完援助非洲難民呀,干么為難自己!難怪俺一只要喝牛?
            奶就拉肚子的朋友從猶大轉(zhuǎn)到紐約了,保命要緊呀。)?
            2)不得在正在執(zhí)行急救任務(wù)的救護(hù)車后座上Make?Love。(這好理解,怕病人看見血管?
            爆裂么!哈哈)?

            posted @ 2006-08-25 17:53 blues 閱讀(548) | 評(píng)論 (0)編輯 收藏

            2006年8月23日

            上海印象

            posted @ 2006-08-23 15:21 blues 閱讀(383) | 評(píng)論 (1)編輯 收藏
            僅列出標(biāo)題  
            国产免费福利体检区久久| 性做久久久久久久| 久久婷婷人人澡人人| 亚洲日本久久久午夜精品| 99久久夜色精品国产网站| 日本人妻丰满熟妇久久久久久| 97久久久精品综合88久久| 欧美一级久久久久久久大| 亚洲精品蜜桃久久久久久| 青青热久久综合网伊人| 午夜精品久久久久久影视riav| 国产亚洲欧美精品久久久| 久久无码国产| 亚洲国产精品久久久久婷婷软件| 2021久久精品免费观看| 狠狠色丁香婷婷综合久久来来去| 久久亚洲私人国产精品vA| 亚洲欧洲中文日韩久久AV乱码| AV狠狠色丁香婷婷综合久久 | 久久久久成人精品无码中文字幕 | 亚洲精品蜜桃久久久久久| 久久黄视频| 97久久综合精品久久久综合| 久久99精品国产麻豆宅宅| 久久毛片免费看一区二区三区| 亚洲成色999久久网站| 99精品久久精品| 久久久精品人妻一区二区三区四| 色综合久久夜色精品国产| 久久国产视屏| 久久人搡人人玩人妻精品首页| 久久久国产精品福利免费| 国产精品免费看久久久| 精品无码久久久久国产| 久久精品无码专区免费东京热| 午夜精品久久久久久99热| 久久人人爽人人爽人人片av高请 | www性久久久com| 久久国产成人精品麻豆| 久久综合九色综合精品| 久久精品成人免费观看97|