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 梦在天涯 阅读(4221) 评论(1)  编辑 收藏 引用 所属分类: CPlusPlusManage c++ /CLI

评论

# re: C++/CLI 2007-06-04 14:40 看图软件

说的非常对  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

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

搜索

  •  

积分与排名

  • 积分 - 1784889
  • 排名 - 5

最新评论

阅读排行榜