• <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#基礎}

            C++/CLI


            前言:為了介紹C#寫界面,C++寫算法的快捷交互開發方式,首先介紹c++,C#內部的DLL,COM調用.

            一,概念
            C++/CLI,結合和Native C++的.Net的特性,使我們可以在C++/CLI中更方便的使用Native C++和.Net資源,更快捷的開發,所以有人說C++/CLI是有史
            以來最強大的語言,但是強大也有他的弊端,就是復雜。也正是因為他的復雜所以許多C++的開發人員轉到。net的時候,并不是去學習C++/CLI
            ,而是學習C#,但是如果我們了解一些C++/CLI的知識,將使我們混合使用C++的。net編程更游刃有余。
            C++/CLI通過了歐盟的ECMA標準,可以訪問和免費下載標準文檔:
                   http://www.ecma-international.org/publications/standards/Ecma-372.htm (這點比C++好多了!~)


            二,不同
            1)C++/CLI同時支持C++語言特性和.NET編程語言特性,可以說是Native C++和.Net的一個并集。

            2)基礎類型不僅可以使用Native C++的,還可以使用.Net的。

            3)除了Native的特性,還可以使用可以使用.Net的屬性,代理,事件等特性。

            4)Native類型: class{} ,struct{}。

            5)CLR類型:value class{},value struct{},ref class{},ref struct{}。

            6)使用gcnew構造的ref類型的對象handle,是托管的對象,內存將被自動釋放。

            7)除了有Pointer,還增加了hanlde,注意null,nullptr與pointer和handle的對應使用。

            8)ref class type R除了析構函數~R(){},還必須有finalizer函數!R(){}。

            9)Native類型可以多繼承,ref類型單類繼承,多接口繼承。

            10)除了Native中的函數重載,ref類型中有override顯示的重載和命名重載。

            11)不經可以使用Native的數組,還可以使用CLR的數組System::Array^.

            12) enum 增加了訪問屬性和類型屬性,CLR的enum定義 enum class R{} 或 enum struct R{}.

            13)仍然有C++的模版,泛型特性,還有.Net的程序集,元素據等概念。

            14)可以使用#pragma managed和#pragma unmanaged來控制編譯為native或clr。

            15)C++/CLI的本質可以借用別人的一句話來說明:“.NET的歸.NET,C++的還歸C++!”。


            三,實例
            1)

            #include "stdafx.h"

            using namespace System;

            //---------------------------------------------------
            public struct PointN
            {    
            };
            public class LineN
            {
            private:
                PointN m_firstPoint;
                PointN m_endPoint;
            public :
                LineN(PointN first,PointN 
            end)
                {
                    m_firstPoint 
            = first;
                    m_endPoint 
            = end;
                }
            };
            //---------------------------------------------------
            public value struct PointV
            {
            };
            public value class LineV
            {
            private:
                PointV m_firstPoint;
                PointV m_endPoint;
            public :
                LineV(PointV first,PointV 
            end)
                {
                    m_firstPoint 
            = first;
                    m_endPoint 
            = end;
                }
            }; 
            //----------------------------------------------------
            public ref struct PointR
            {    
            };
            public ref class LineR
            {
            private:
                PointR m_firstPoint;
                PointR m_endPoint;
            public :
                LineR(PointR
            ^ first,PointR^ end)
                {
                    
            //m_firstPoint = first;
                    
            //m_endPoint = end;
                }
            };
            //----------------------------------------------------

            int main(array<System::String ^> ^args)
            {
                PointN pointn;
                LineN linen(pointn,pointn);
                LineN
            * linenp = new LineN(pointn,pointn);
                delete linenp;
                
            //LineN^ linenh = gcnew LineN(pointn,pointn); //error

                PointV pointv;
                LineV linev(pointv,pointv);
                LineV
            * linevp = new LineV(pointv,pointv);
                delete linevp;
                LineV
            ^ linevh = gcnew LineV(pointv,pointv);    

                    
                PointR
            ^ pointr2 = gcnew PointR();
                LineR liner(pointr2,pointr2);
                LineR
            ^ linerh = gcnew LineR(pointr2,pointr2);
                
            //LineR* linerp = new LineR(pointr2,pointr2); //error
                
            //delete linerp;

                Console::WriteLine(L
            "Hello World");
                return 
            0;
            }

            // Native 類型,可以定義一般變量和指針,不可以定義句柄。
            // value 類型,可以定義一般變量,指針和句柄。
            // Ref 類型,可以定義一般變量和句柄,但是不可以定義指針。

            2)

            #include "stdafx.h"
            using namespace System;
            //--------------------------------------------------
            interface class IBase
            {
            };

            ref class Derived1 : 
            private IBase {}; //error C3141
            ref class Derived2 : protected IBase {}; 
            //error C3141
            ref class Derived3 : IBase {}; 
            //public assumed
            //-------------------------------------------------
            ref class RefBase {};
            value class ValBase {};
            interface class IBase2 {};

            value class Derived1 : RefBase {}; 
            //error C3830
            value class Derived2 : ValBase {}; 
            //error C3830
            value class Derived3 : IBase {};
            //---------------------------------------------------

            int main(array<System::String ^> ^args)
            {
                Console::WriteLine(L
            "Hello World");
                return 
            0;
            }

            // 對interface只能public繼承。
            // value type 只能從interface繼承。
            3)

            #include "stdafx.h"

            using namespace System;
            #define Show(x) Console::WriteLine(x)

            public class N
            {
            public:
               N()
               {
                  Show(
            "N::ctor");
               }
               ~N()
               {
                  Show(
            "N::dtor");
               }
            };

            public ref class R
            {
            public:
                void Test1(
            int x)
                {
                    
            array<String^>^ strarray = gcnew array<String^>(x);
                    
            for(int i=0; i<x; i++)
                        strarray[i] 
            = String::Concat("Number ",i.ToString());
                    
            for(int i=0; i<x; i++)
                        Console::WriteLine(strarray[i]);
                }
                void Test2(
            int x)
                {
                    
            array<int>^ strarray = gcnew array<int>(x);
                    
            for(int i=0; i<x; i++)
                        strarray[i] 
            = i * 10;
                    
            for(int i=0; i<x; i++)
                        Console::WriteLine(strarray[i]);
                }
                void Test3()
                {
                    
            array<String^,2>^ names = gcnew array<String^,2>(4,2);
                    names[
            0,0= "John";
                    names[
            1,0= "Tim";
                    names[
            2,0= "Nancy";
                    names[
            3,0= "Anitha";
                    
            for(int i=0; i<4; i++)
                        
            if(i%2==0)
                            names[i,
            1= "Brown";
                        
            else
                            names[i,
            1= "Wilson";
                    
            for(int i=0; i<4; i++)
                        Console::WriteLine(
            "{0} {1}",names[i,0],names[i,1]);
                }    
                void Test4()
              {
                
            array<array<int>^>^ arr = gcnew array<array<int>^> (5); 

                
            for(int i=0, j=10; i<5; i++, j+=10)
                {
                  arr[i] 
            = gcnew array<int> (j);
                }
                Console::WriteLine(
            "Rank = {0}; Length = {1}",
                  arr
            ->Rank,arr->Length);
                
            for(int i=0; i<5; i++)
                  Console::WriteLine(
            "Rank = {0}; Length = {1}",
                    arr[i]
            ->Rank,arr[i]->Length);   
              }
                void Test5()
               {
                  
            array<N*>^ arr = gcnew array<N*>(3);
                  
            for(int i=0; i<arr->Length; i++)   
                     arr[i] 
            = new N();
               }
                 void Test6(
            String^ s, [ParamArray] array<int>^ arr )    
                {
                    Console::Write(s);
                    
            for(int i=0; i<arr->Length; i++)
                        Console::Write(
            " {0}",arr[i]);
                    Console::WriteLine();
                }
                 void Test7()
              {
                
            //Single dimensional arrays
                
            array<String^>^ arr1 = gcnew array<String^> {"Nish""Colin"};
                
            array<String^>^ arr2 = {"Nish""Smitha"};
                
                
            //Multi dimensional arrays
                
            array<Object^,2> ^ multiobarr = {{"Nish"100}, {"Jambo"200}};

                Console::WriteLine(arr1.Length);
              }
            };
            int main(array<System::String ^> ^args)
            {
                R
            ^ r = gcnew R();
                r
            ->Test1(5);
                r
            ->Test2(5);
                r
            ->Test3();
                r
            ->Test4();
                r
            ->Test5();
                r
            ->Test6("Nish",1,25,100);
                r
            ->Test7();

                Console::WriteLine(L
            "Hello World");
                return 
            0;
            }



            四,參考:C++/CLI中類的本質分析:       http://blog.csdn.net/Changjiang/archive/2006/11/27/1415972.aspx
                                A first look at C++/CLI:            http://www.codeproject.com/managedcpp/cppcliintro01.asp 
                                System::Array:                              http://www.codeproject.com/managedcpp/cppcliarrays.asp

            posted on 2007-05-29 13:38 夢在天涯 閱讀(4253) 評論(1)  編輯 收藏 引用 所屬分類: CPlusPlusManage c++ /CLI

            評論

            # re: C++/CLI 2007-06-04 14:40 看圖軟件

            說的非常對  回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

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

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804159
            • 排名 - 5

            最新評論

            閱讀排行榜

            无码人妻久久一区二区三区蜜桃| 久久人人爽人人爽人人片av高请| 亚洲国产精品久久久久婷婷老年 | 久久高潮一级毛片免费| 国产精品美女久久久| 国产精品99久久久久久董美香| 久久精品二区| 久久天天躁狠狠躁夜夜96流白浆| 久久精品人人做人人爽电影| 噜噜噜色噜噜噜久久| 久久久久无码精品国产| 亚洲国产精品无码久久青草 | 亚洲精品乱码久久久久久按摩| 久久九九有精品国产23百花影院| 久久久久久无码国产精品中文字幕| 精品国产乱码久久久久软件 | 精品国际久久久久999波多野| 99久久精品国产毛片| 国产99久久久国产精品小说| 国产精品视频久久久| 狠狠色丁香久久婷婷综合蜜芽五月| 久久久九九有精品国产| 久久人人爽人人爽人人av东京热| 91精品国产综合久久香蕉 | 尹人香蕉久久99天天拍| 97久久香蕉国产线看观看| 欧美一区二区久久精品| 国产91久久综合| 久久青青草原国产精品免费| 天堂久久天堂AV色综合| 女人高潮久久久叫人喷水| 久久精品国产精品亚洲人人 | 久久亚洲AV成人无码国产 | 99久久99久久精品国产片果冻| 国产精品久久久99| 国产精品岛国久久久久| 久久久久亚洲AV无码麻豆| 东方aⅴ免费观看久久av| 久久久久久久精品成人热色戒| 久久精品无码一区二区日韩AV| 18岁日韩内射颜射午夜久久成人|