• <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++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            C++/CLI


            前言:為了介紹C#寫界面,C++寫算法的快捷交互開發(fā)方式,首先介紹c++,C#內(nèi)部的DLL,COM調(diào)用.

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


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

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

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

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

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

            6)使用gcnew構(gòu)造的ref類型的對象handle,是托管的對象,內(nèi)存將被自動釋放。

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

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

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

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

            11)不經(jīng)可以使用Native的數(shù)組,還可以使用CLR的數(shù)組System::Array^.

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

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

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

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


            三,實(shí)例
            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中類的本質(zhì)分析:       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 夢在天涯 閱讀(4254) 評論(1)  編輯 收藏 引用 所屬分類: CPlusPlusManage c++ /CLI

            評論

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

            說的非常對  回復(fù)  更多評論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

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

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804430
            • 排名 - 5

            最新評論

            閱讀排行榜

            要久久爱在线免费观看| 伊人久久综合成人网| 久久精品国产亚洲AV电影| 一个色综合久久| 久久亚洲AV无码精品色午夜麻豆 | 久久综合亚洲色HEZYO国产| 人人狠狠综合久久亚洲婷婷| 久久AV高清无码| 国内精品久久久久久野外| 国产精品久久久亚洲| 国产精品一久久香蕉产线看| 久久人妻少妇嫩草AV无码专区| 中文字幕无码免费久久| 蜜臀av性久久久久蜜臀aⅴ | 精品久久久久久久无码| 一本一本久久A久久综合精品 | 久久久久国产视频电影| 精品久久久久久久中文字幕| 久久精品国产亚洲精品| 久久亚洲国产最新网站| 亚洲综合日韩久久成人AV| 色综合久久中文字幕无码| AV狠狠色丁香婷婷综合久久 | 伊人久久大香线蕉综合5g| 久久免费看黄a级毛片| 欧美大香线蕉线伊人久久| 亚洲欧美日韩精品久久| 亚洲国产成人精品91久久久| 亚洲精品乱码久久久久久| 久久99精品国产99久久6男男| 国产精品免费久久久久电影网| 香蕉久久影院| 久久人人爽人人爽人人AV| 久久精品人人做人人爽电影| 久久久中文字幕日本| 嫩草伊人久久精品少妇AV| 国产午夜精品久久久久九九| 久久经典免费视频| 久久久久久久综合日本亚洲| 欧美色综合久久久久久 | 国产精品久久久久久久久|