2013年4月28日 星期日

DIY - 智能小車:Microsoft Visual C++/CLI 介紹 (二十五)

DIY - 智能小車:Microsoft Visual C++/CLI 介紹 (二十五)

筆者已往都是使用 C/Visual C++ 的語言來編寫程式,雖然仍是不能夠完全掌握,一般編寫 C 程式是需要時間來獲得知識和經驗,在測試 S605 Webcam 使用了 C# 來編程,感覺 C# 加上 .NET 的簡單直接而強大,雖然 C# 語言就比較陌生,但 .NET Framwork 是非常好的框架,所以考慮使用新的 C++/CLI 來編寫程式。

Microsoft Visual C++/CLI
C++/CLI CLICommon Language Infrastructure,通用語言框架)是一門用來代替 C++ 託管擴展新的語言規範,C++ 是電腦程式設計語言,它是一種靜態資料類型檢查的,支援多範型的通用程式設計語言。CLI 提供了一套可執行代碼和它所運行需要的虛擬執行環境的規範。CLI 是作為作業系統和應用程式中間的抽象層,簡單而言 C++/CLI 除了包含 C++ 和  CLI 擴展,最重要是C++ .NET 的無縫連接,就是將新與舊的東西結合在一起。


2003106,歐洲電腦製造商協會(ECMAEuropean Computer Manufacturers Association)宣佈成立專家組,負責結合 ISO 標準 C++ 與通用語言,開發一個可擴展語言的標準,這個新的可擴展語言被稱為 C++/CLI 標準。C++/CLI 現在可以被 Visual C++ 2005 和更高版本的編譯器支援。C++/CLI 的部分特性已經申請了專利。

ECMA Logo

C# C++/CLI 的語法:
Items
C#
C++/CLI
Create a new object
object obj = new object();
Object^ obj = gcnew Object();
Create a strings
String s = "Hello";
String ^s = "Hello";
Create a integer
int len = s.Length;
int len = s->Length;
Create an array
int[] integers = {1, 2, 3, 4};
array^ integers = {1, 2, 3, 4};
Create an array
int[,] moreIntegers = { {5,6}, {7, 8} };
array^ moreIntegers = { {5,6}, {7, 8} };
Create an array
string[] strings = new string[2]{"Hello", "World"};
array^ strings = gcnew array(2){"Hello", "World"};
Create a subroutine
static void RunFoo(Func foo)
void RunFoo(function foo)

Allocate on native heap

N* pn = new N;
Allocate on CLI heap

R^ hr = gcnew R;
Bind ordinary reference to native object

N& rn = *pn;
Bind tracking reference to gc-lvalue

R% rr = *hr;
Null
something = null;
something = nullptr;
Customer Cursor
This.Cursor
This->Cursor=gcnew System::Windows::Cursor::UpArrow


以上是主要 C++/CLI 語法,其實還有很多很多!暫不能完全了解

C# 程式:
// C#
using System;

enum Gender
{
    Male,
    Female
}

class Person
{
    public string Name;
    public int Age;
    public Gender Sex;

    public Person(string name, int age, Gender sex)
    {
        Name = name;
        Age = age;
        Sex = sex;
    }

    public override string ToString()
    {
        return String.Format("Name = {0, -10} Age = {1}  Sex = {2}", Name, Age, Sex.ToString()[0]);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Person[] persons = new Person[4];
        persons[0] = new Person("John", 25, Gender.Male);
        persons[1] = new Person("Freda", 35, Gender.Female);
        persons[2] = new Person("Bill", 45, Gender.Male);
        persons[3] = new Person("Danielle", 55, Gender.Female);
        foreach (Person person in persons) Console.WriteLine(person);
        Console.ReadKey();
    }
}

C++/CLI 程式:
// C++/CLI
// personlister.cpp : main project file.

#include "stdafx.h" // standard header

using namespace System;

enum class Gender
{
    Male,
    Female
};

ref class Person
{
public:
    String^ Name;
    int Age;
    Gender Sex;

    Person(String^ name, int age, Gender sex)
    {
        Name = name;
        Age = age;
        Sex = sex;
    }

    virtual String^ ToString() override
    {
        return String::Format("Name = {0, -10} Age = {1}  Sex = {2}", Name, Age, Sex.ToString()[0]);
    }
};

int main(array ^args)
{
    array^ persons = gcnew array(4);
    persons[0] = gcnew Person("John", 25, Gender::Male);
    persons[1] = gcnew Person("Freda", 35, Gender::Female);
    persons[2] = gcnew Person("Bill", 45, Gender::Male);
    persons[3] = gcnew Person("Danielle", 55, Gender::Female);
    for each (Person^ person in persons) Console::WriteLine(person);
    Console::ReadKey();
    return 0;
}

The output of both programs is:
Name = John Age = 25 Sex = M
Name = Freda Age = 35 Sex = F
Name = Bill Age = 45 Sex = M
Name = Danielle Age = 55 Sex = F

參考網址: 
 
2013428 天氣報告
氣溫:23.4 @ 21:50
相對濕度:百分之91%
天氣:微雨

沒有留言:

張貼留言