前言:為了介紹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