筆者已往都是使用 C/Visual C++ 的語言來編寫程式,雖然仍是不能夠完全掌握,一般編寫 C 程式是需要時間來獲得知識和經驗,在測試 S605 Webcam 使用了 C# 來編程,感覺 C# 加上 .NET 的簡單直接而強大,雖然 C# 語言就比較陌生,但 .NET Framwork 是非常好的框架,所以考慮使用新的 C++/CLI 來編寫程式。
Microsoft Visual C++/CLI |
2003年10月6日,歐洲電腦製造商協會(ECMA,European 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
|
Create an array
|
int[,] moreIntegers = { {5,6},
{7, 8} };
|
array
|
Create an array
|
string[] strings = new
string[2]{"Hello", "World"};
|
array
|
Create a subroutine
|
static void RunFoo(Func
|
void
RunFoo(function
|
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
{
array
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
參考網址:
2013年4月28日 天氣報告
氣溫:23.4 度 @ 21:50
相對濕度:百分之91%
天氣:微雨
沒有留言:
張貼留言