• <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++ 高級(jí)} {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的知識(shí),將使我們混合使用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)存將被自動(dòng)釋放。

            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 夢在天涯 閱讀(4259) 評(píng)論(1)  編輯 收藏 引用 所屬分類: CPlusPlus 、Manage c++ /CLI

            評(píng)論

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

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

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807602
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            精品久久久久久亚洲精品| 久久婷婷午色综合夜啪| 人妻久久久一区二区三区| 亚洲国产精品久久电影欧美| 久久亚洲春色中文字幕久久久| 国产精品99久久免费观看| 国产激情久久久久影院老熟女免费 | 久久久青草久久久青草| 精品久久久久久国产三级| 亚洲综合久久夜AV | 国产精品一久久香蕉国产线看观看| 久久综合九色综合97_久久久| 久久精品桃花综合| 亚洲国产精品久久66| 思思久久精品在热线热| 热99re久久国超精品首页| 久久精品日日躁夜夜躁欧美| 国产精品免费久久久久久久久| 伊人久久大香线蕉AV一区二区| 亚洲午夜精品久久久久久人妖| 亚洲成色WWW久久网站| 一本久久免费视频| 九九久久精品国产| 久久精品成人免费网站| 人妻无码αv中文字幕久久| 要久久爱在线免费观看| 久久国产精品免费一区| 久久久久一区二区三区| 久久99国产精品二区不卡| 久久综合给合久久狠狠狠97色69| 色99久久久久高潮综合影院| 久久精品成人欧美大片| 99精品伊人久久久大香线蕉| 狠狠色丁香婷婷综合久久来 | 国产精品久久99| 麻豆亚洲AV永久无码精品久久| 久久亚洲日韩看片无码| 亚洲七七久久精品中文国产| 伊人精品久久久久7777| 国产香蕉久久精品综合网| 久久久久亚洲AV片无码下载蜜桃|