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

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            Managed c++與c++\CLI的區(qū)別(實例)

            1 Managed c++ (使用vs2002,2003,建立visual c++下的console application(.net ) project,project還有各種類型)
            ?? 注: 此時的project setting里一定是 :using Managed Extenstions ->Yes

            簡單實例

            #using?<mscorlib.dll>
            using?namespace?System;

            __gc?
            class?G?
            {
            public:
            ????
            int?k;
            ????
            int?sum(int);
            }
            ;

            G::sum(
            int?i)?{return?i*(i?+?1)/2;}

            int?main()
            {
            ????G?
            *?g?=?new?G;
            ????Console::WriteLine(g
            ->sum(4));?//?結果輸出10
            ????return?0;
            }
            ?
            接口實例

            #using?<mscorlib.dll>
            using?namespace?System;

            __gc?__interface?Ibase1?
            {
            ????
            int?f(int);
            }
            ;
            __gc?__interface?Ibase2?
            {
            ????
            int?f(int);
            }
            ;
            __gc?
            struct?C:?Ibase1,?Ibase2?{
            ????
            int?f(int?i)?{?//?接口方法的實現(xiàn)
            ????????return?2*i-1;
            ????}
            ;
            }
            ;

            int?main(void){
            ????C
            *?c?=?new?C;
            ????Console::WriteLine((c?
            ->?f(1)).ToString());?//?輸出結果為1
            ????Console::WriteLine((__try_cast?(c)->f(2)).ToString());?
            ????
            //?輸出結果為3

            ????Console::WriteLine((__try_cast?(c)
            ->f(3)).ToString());
            ????
            //?輸出結果為5

            ????
            return?0;
            }

            property實例:

            #using?<mscorlib.dll>
            using?namespace?System;

            __gc?
            class?G?{
            public:
            ????__property?
            int?get_Size()?{
            ????????Console::WriteLine(S
            "get_屬性");
            ????????
            return?nSize;
            ????}
            ;
            ????__property?
            void?set_Size(int?i)?{?
            ????????Console::WriteLine(S
            "set_屬性");
            ????????nSize?
            =?i;
            ????}
            ;
            private:
            ????
            int?nSize;
            }
            ;

            int?main()?{
            ????G?
            *?pG?=?new?G;
            ????pG
            ->Size?=?10;?//?調(diào)用set_Size
            ????int?i?=?pG->Size;?//?調(diào)用get_Size
            ????Console::WriteLine(i);
            }
            ?

              
            //程序結果為:

            ?????
            //?   set_屬性

            ????????
            //?   get_屬性

            ????????
            //????   10

            代理實例:
            #using?<mscorlib.dll>
            using?namespace?System;

            __delegate?
            int?GetDayOfWeek();?//?委派方法的聲明
            __gc?class?MyCalendar
            {
            public:
            ????MyCalendar()?:?m_nDayOfWeek(
            4)?{}
            ????
            int?MyGetDayOfWeek()?{
            ????????Console::WriteLine(
            "非靜態(tài)方法");?
            ????????
            return?m_nDayOfWeek;?
            ????}

            ????
            static?int?MyStaticGetDayOfWeek()?{?
            ????????Console::WriteLine(
            "靜態(tài)方法");?
            ????????
            return?6;?
            ????}

            private:
            ????
            int?m_nDayOfWeek;
            }
            ;

            int?main(void)
            {
            ????GetDayOfWeek?
            *?pGetDayOfWeek;?//?聲明委派類型變量
            ????int?nDayOfWeek;

            ????
            //?將類的靜態(tài)方法MyStaticGetDayOfWeek綁定成委派
            ????pGetDayOfWeek?=?new?GetDayOfWeek(0,?&MyCalendar::MyStaticGetDayOfWeek);
            ????nDayOfWeek?
            =?pGetDayOfWeek->Invoke();?//?委派的調(diào)用
            ????Console::WriteLine(nDayOfWeek);

            ????
            //?將一個類的實例綁定成委派
            ????MyCalendar?*?pcal?=?new?MyCalendar();
            ????pGetDayOfWeek?
            =?
            ????????static_cast(Delegate::Combine(pGetDayOfWeek,
            ????????
            new?GetDayOfWeek(pcal,?&MyCalendar::MyGetDayOfWeek)));
            ????nDayOfWeek?
            =?pGetDayOfWeek->Invoke();
            ????Console::WriteLine(nDayOfWeek);

            ????
            //?刪除綁定委派的類實例
            ????pGetDayOfWeek?=?
            ????????static_cast(Delegate::Remove(pGetDayOfWeek,
            ????????
            new?GetDayOfWeek(pcal,?&MyCalendar::MyGetDayOfWeek)));

            ????
            return?0;
            }
            ?

             
            /* 輸出結果是:

            ??????   靜態(tài)方法
            ?????????  
            ?????????   6

            ????????????   靜態(tài)方法

            ???????????????   非靜態(tài)方法

            ??????????????????   4
            */


            2 c++\CLI (使用vs2005 ,之前的版本不支持,建立visual c++下的CLR Console Application ,當然還有其他的類型可選,比如CLR empty ...^_^,)
            ? 注:此時project setting 中common language runtime support : Common Language Runtime Support (/clr) ,此時的project中已經(jīng)ref system....DLL,所以不用想上面那樣#using <mscordll>

            簡單實例

            #include?"stdafx.h"
            using?namespace?System;

            int?main(array<System::String?^>?^args)
            {
            ????String
            ^?str?=?gcnew?String("Hello?World");
            ????Object
            ^?o1?=?gcnew?Object();
            ????Console::WriteLine(str);
            ????
            return?0;
            }

            interface 實例

            using?namespace?System;

            interface?class?IDog
            {
            ????
            void?Bark();
            }
            ;

            ref?class?Dog?:?IDog
            {
            public:
            ????
            void?Bark()
            ????
            {
            ????????Console::WriteLine(
            "Bow?wow?wow");
            ????}

            }
            ;

            void?_tmain()
            {
            ????Dog
            ^?d?=?gcnew?Dog();
            ????d
            ->Bark();
            }


            property實例

            ref?class?Base
            {
            public:
            ????
            ref?struct?SBaseData
            ????
            {
            ????
            public:
            ????????SBaseData()
            ????????
            {
            ????????????x?
            =?10;
            ????????}

            ????????SBaseData(
            int?n)
            ????????
            {
            ????????????x?
            =?n;
            ????????}

            ????????
            int?x;????????
            ????}
            ;
            private:
            ????SBaseData
            ^?_bd;
            public:
            ????Base()
            ????
            {
            ????????_bd?
            =?gcnew?SBaseData();
            ????}

            ????
            virtual?property?SBaseData^?Data
            ????
            {
            ????????SBaseData
            ^?get()
            ????????
            {
            ????????????
            return?_bd;
            ????????}

            ????????
            void?set(SBaseData^?val)
            ????????
            {
            ????????????_bd
            ->x?=?val->x;
            ????????}

            ????}

            }
            ;
            ref?class?Derived?:?Base
            ????
            {
            ????
            public:
            ????????
            ref?struct?SDerivedData?:?Base::SBaseData
            ????????
            {
            ????????
            public:
            ????????????SDerivedData(
            int?n1,?int?n2)?:?SBaseData(n1)
            ????????????
            {
            ????????????????y?
            =?n2;
            ????????????}

            ????????????SDerivedData()?:?SBaseData()
            ????????????
            {
            ????????????????y?
            =?100;
            ????????????}

            ????????????
            int?y;????????
            ????????}
            ;
            ????
            private:
            ????????SDerivedData
            ^?_bd;
            ????
            public:
            ????????Derived()
            ????????
            {
            ????????????_bd?
            =?gcnew?SDerivedData();
            ????????}


            ????????
            virtual?property?SBaseData^?Data
            ????????
            {
            ????????????SBaseData
            ^?get()?override?//=?Base::Data::get
            ????????????{
            ????????????????
            return?_bd;
            ????????????}

            ????????????
            void?set(SBaseData^?val)?override?//=?Base::Data::set
            ????????????{
            ????????????????
            try
            ????????????????
            {
            ????????????????????_bd
            ->x?=?val->x;
            ????????????????????_bd
            ->y?=?safe_cast<SDerivedData^>(val)->y;
            ????????????????}

            ????????????????
            catch(System::InvalidCastException?^)
            ????????????????
            {
            ????????????????????
            //log?an?error
            ????????????????}

            ????????????}

            ????????}

            ????????
            virtual?property?SDerivedData^?SData
            ????????
            {
            ????????????SDerivedData
            ^?get()??
            ????????????
            {
            ????????????????
            return?_bd;
            ????????????}

            ????????????
            void?set(SDerivedData^?val)?
            ????????????
            {
            ????????????
            ????????????????_bd
            ->x?=?val->x;
            ????????????????_bd
            ->y?=?val->y;
            ????????????????????????????
            ????????????}

            ????????}

            ????}
            ;
            int?main(array<System::String?^>?^args)

            {
            ????Derived
            ^?d?=?gcnew?Derived;
            ????
            //Base^?d?=?gcnew?Derived;???//?error?,?y?is?not?a?member?of?Base?class
            ????int?num?=?d->Data->x;
            ????
            int?num2?=?d->SData->y;
            ????System::Console::WriteLine(num);
            ????System::Console::WriteLine(num2);
            ????
            return?0;
            }


            index實例

            using?namespace?System;
            using?namespace?System::Collections;
            ref?class?R
            {
            private:
            ????Hashtable
            ^?h;
            public:
            ????R()
            ????
            {
            ????????h?
            =?gcnew?Hashtable();
            ????}


            ????
            //Named?property
            ????property?int?Age[String^]
            ????
            {
            ????
            protected:
            ????????
            int?get(String^?s)
            ????????
            {
            ????????????
            if(h->ContainsKey(s))
            ????????????
            {
            ????????????????
            for?each(DictionaryEntry?de?in?h)
            ????????????????
            {
            ????????????????????
            if(s->CompareTo(de.Key)?==?0)
            ????????????????????????
            return?(int)de.Value;
            ????????????????}

            ????????????}

            ????????????
            return?0;
            ????????}
            ????

            ????????
            void?set(String^?s,?int?age)
            ????????
            {
            ????????????h
            ->Add(s,age);
            ????????}

            ????}


            ????
            //Default?property
            ????property?int?default[String^]
            ????
            {
            ????????
            int?get(String^?s)
            ????????
            {
            ????????????
            return?Age[s];
            ????????}


            ????????
            void?set(String^?s,?int?age)
            ????????
            {
            ????????????Age[s]?
            =?age;
            ????????}

            ????}
            ????
            }
            ;

            int?main(array<System::String?^>?^args)

            {
            ????R
            ^?r?=?gcnew?R();

            ????r[
            "Nish"]?=?27;
            ????r[
            "Smitha"]?=?15;
            ????r[
            "Chris"]?=?21;
            ????r[
            "Don"]?=?87;

            ????Console::WriteLine(r[
            "Nish"]);
            ????Console::WriteLine(r[
            "George"]);
            ????Console::WriteLine(r[
            "Smitha"]);
            ????Console::WriteLine(r[
            "Don"]);
            ????Console::WriteLine(r[
            "Bert"]);
            ????
            return?0;

            posted on 2006-08-18 10:58 夢在天涯 閱讀(3574) 評論(0)  編輯 收藏 引用 所屬分類: CPlusPlusManage c++ /CLI

            公告

            EMail:itech001#126.com

            導航

            統(tǒng)計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804303
            • 排名 - 5

            最新評論

            閱讀排行榜

            国产高潮久久免费观看| 久久精品一本到99热免费| 精品久久久久久国产潘金莲 | 人妻少妇精品久久| 久久人人爽人爽人人爽av| 91精品国产91热久久久久福利| 国产69精品久久久久777| 久久久久久夜精品精品免费啦| 无码人妻少妇久久中文字幕蜜桃| 欧美日韩精品久久久免费观看| 亚州日韩精品专区久久久| 亚洲Av无码国产情品久久| 久久久久国产精品嫩草影院| 国产偷久久久精品专区| 久久亚洲精品人成综合网| 国产精品久久久久久吹潮| 国产精品成人久久久久三级午夜电影| 曰曰摸天天摸人人看久久久| 久久久久国产日韩精品网站 | 亚洲日韩欧美一区久久久久我| 亚洲欧美一区二区三区久久| 狠狠综合久久AV一区二区三区| 一本一道久久综合狠狠老| 99国产精品久久| 久久婷婷五月综合成人D啪| 久久久久精品国产亚洲AV无码| 九九99精品久久久久久| 亚洲午夜精品久久久久久浪潮| 久久精品亚洲中文字幕无码麻豆| 国产日韩久久久精品影院首页| 怡红院日本一道日本久久 | 国产综合久久久久| 精品久久久久久99人妻| 久久狠狠爱亚洲综合影院| 久久99精品国产| 国产美女亚洲精品久久久综合| 欧美亚洲另类久久综合| 狠狠色狠狠色综合久久| 精品国产91久久久久久久a | 99久久国产综合精品五月天喷水| 久久精品日日躁夜夜躁欧美|