青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

A first look at C++/CLI(C++的管理擴(kuò)展)

Introduction

When Microsoft brought out the Managed Extensions to C++ with VS.NET 7, C++ programmers accepted it with mixed reactions. While most people were happy that they could continue using C++, nearly everyone was unhappy with the ugly and twisted syntax offered by Managed C++. Microsoft obviously took the feedback it got very seriously and they decided that the MC++ syntax wasn't going to be much of a success.

On October 6th 2003, the ECMA announced the creation of a new task group to oversee development of a standard set of language extensions to create a binding between the ISO standard C++ programming language and Common Language Infrastructure (CLI). It was also made known that this new set of language extensions will be known as the C++/CLI standard, which will be supported by the VC++ compiler starting with the Whidbey release (VS.NET 2005).

Problems with the old syntax

  • Ugly and twisted syntax and grammar - All those double underscores weren't exactly pleasing to the eye.
  • Second class CLI support - Compared to C# and VB.NET, MC++ used contorted workarounds to provide CLI support, for e.g. it didn't have a for-each construct to enumerate .NET collections.
  • Poor integration of C++ and .NET - You couldn’t use C++ features like templates on CLI types and you couldn’t use CLI features like garbage collection on C++ types.
  • Confusing pointer usage - Both unmanaged C++ pointers and managed reference pointers used the same * based syntax which was quite confusing because __gc pointers were totally different in nature and behavior from unmanaged pointers.
  • The MC++ compiler could not produce verifiable code

What C++/CLI gives us?

  • Elegant syntax and grammar -This gave a natural feel for C++ developers writing managed code and allowed a smooth transition from unmanaged coding to managed coding. All those ugly double underscores are gone now.
  • First class CLI support - CLI features like properties, garbage collection and generics are supported directly. And what's more, C++/CLI allows jus to use these features on native unmanaged classes too.
  • First class C++ support - C++ features like templates and deterministic destructors work on both managed and unmanaged classes. In fact C++/CLI is the only .NET language where you can *seemingly* declare a .NET type on the stack or on the native C++ heap.
  • Bridges the gap between .NET and C++ - C++ programmers won't feel like a fish out of water when they attack the BCL
  • The executable generated by the C++/CLI compiler is now fully verifiable.

Hello World

				using
				namespace System;

void _tmain()
{
    Console::WriteLine("Hello World");
}

Well, that doesn't look a lot different from old syntax, except that now you don't need to add a reference to mscorlib.dll because the Whidbey compiler implicitly references it whenever you compile with /clr (which now defaults to /clr:newSyntax).

Handles

One major confusion in the old syntax was that we used the * punctuator with unmanaged pointers and with managed references. In C++/CLI Microsoft introduces the concept of handles.

				void _tmain()
{
    //The ^ punctuator represents a handle
    String^ str = "Hello World";
    Console::WriteLine(str);
}

The ^ punctuator (pronounced as cap) represents a handle to a managed object. According to the CLI specification a handle is a managed object reference. Handles are the new-syntax equivalent of __gc pointers in the MC++ syntax. Handles are not to be confused with pointers and are totally different in nature from pointers.

How handles differ from pointers?

  • Pointers are denoted using the * punctuator while handles are denoted using the ^ punctuator.
  • Handles are managed references to objects on the managed heap, pointers just point to a memory address.
  • Pointers are stable and GC cycles do not affect them, handles might keep pointing to different memory locations based on GC and memory compactions.
  • For pointers, the programmer must deleteexplicitly or else suffer a leak. For handles delete is optional.
  • Handles are type-safe while pointers are most definitely not. You cannot cast a handle to a void^.
  • Just as a new returns a pointer, a gcnew returns a handle.

Instantiating CLR objects

				void _tmain()
{
    String^ str = gcnew String("Hello World");
    Object^ o1 = gcnew Object();
    Console::WriteLine(str);
}

The gcnew keyword is used to instantiate CLR objects and it returns a handle to the object on the CLR heap. The good thing about gcnew is that it allows us to easily differentiate between managed and unmanaged instantiations.

Basically, the gcnew keyword and the ^ operator offer just about everything you need to access the BCL. But obviously you'd need to create and declare your own managed classes and interfaces.

Declaring types

CLR types are prefixed with an adjective that describes what sort of type it is. The following are examples of type declarations in C++/CLI :-

  • CLR types
    • Reference types
      • refclass RefClass{...};
      • refstruct RefClass{...};
    • Value types
      • value class ValClass{...};
      • value struct ValClass{...};
    • Interfaces
      • interfaceclass IType{...};
      • interfacestruct IType{...};
    • Enumerations
      • enumclass Color{...};
      • enumstruct Color{...};
  • Native types
    • class Native{...};
    • struct Native{...};
				using
				namespace System;

interfaceclass IDog
{
    void Bark();
};

refclass Dog : IDog
{
public:
    void Bark()
    {
        Console::WriteLine("Bow wow wow");
    }
};

void _tmain()
{
    Dog^ d = gcnew Dog();
    d->Bark();
}

There, the syntax is now so much more neater to look at than the old-syntax where the above code would have been strewn with double-underscored
?keywords like __gc and __interface.

Boxing/Unboxing

Boxing is implicit (yaay!) and type-safe. A bit-wise copy is performed and an Object is created on the CLR heap. Unboxing is explicit - just do a reinterpret_cast and then dereference.

				void _tmain()
{
    int z = 44;
    Object^ o = z; //implicit boxingint y = *reinterpret_cast<int^>(o); //unboxing

    Console::WriteLine("{0} {1} {2}",o,z,y);

    z = 66; 
    Console::WriteLine("{0} {1} {2}",o,z,y);
}

// Output// 44 44 44// 44 66 44

The Objecto is a boxed copy and does not actually refer the int value-type which is obvious from the output of the second Console::WriteLine.

When you box a value-type, the returned object remembers the original value type.

				void _tmain()
{
    int z = 44;
    float f = 33.567;

    Object^ o1 = z; 
    Object^ o2 = f; 

    Console::WriteLine(o1->GetType());
    Console::WriteLine(o2->GetType());    
}

// Output// System.Int32// System.Single

Thus you cannot try and unbox to a different type.

				void _tmain()
{
    int z = 44;
    float f = 33.567;

    Object^ o1 = z; 
    Object^ o2 = f;

    int y = *reinterpret_cast<int^>(o2);//System.InvalidCastExceptionfloat g = *reinterpret_cast<float^>(o1);
//System.InvalidCastException }

If you do attempt to do so, you'll get a System.InvalidCastException. Talk about perfect type-safety! If you look at the IL generated, you'll see the MSIL box instruction in action. For example :-

				void Box2()
{
    float y=45;
    Object^ o1 = y;
}

gets compiled to :-

.maxstack  1
.locals (float32 V_0, object V_1)

  ldnull
  stloc.1ldc.r4     45.
  stloc.0ldloc.0
  box   [mscorlib]System.Single
  stloc.1ret

According to the MSIL docs, "The box instruction converts the ‘raw’ valueType (an unboxed value type) into an instance of type Object (of type O). This is accomplished by creating a new object and copying the data from valueType into the newly allocated object."

Further reading

Conclusion

Alright, so why would anyone want to use C++/CLI when they can use C#, J# and that VB thingie for writing .NET code? Here are the four reasons I gave during my talk at DevCon 2003 in Trivandrum (Dec 2003).

  • Compile existing C++ code to IL (/clr magic)
  • Deterministic destruction
  • Native interop support that outmatches anything other CLI languages can offer
  • All those underscores in MC++ are gone ;-)

About Nishant Sivakumar



Editor
Site Builder
Nish is a real nice guy living in Toronto who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he has moved to Toronto so he can enjoy the cold winter and get to play in some snow.

He works for The Code Project and handles the Dundas MFC products Ultimate Toolbox, Ultimate Grid and Ultimate TCP/IP that are sold exclusively through The Code Project Storefront. He frequents the CP discussion forums when he is not coding, reading or writing. Nish hopes to visit at least three dozen countries before his human biological mechanism stops working (euphemism used to avoid the use of the d-word here), and regrets that he hasn't ever seen snow until now (will be rectified this December). Oh btw, it must be mentioned that normally Nish is not inclined to speak about himself in the 3rd person.

Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com

Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.

Click here to view Nishant Sivakumar's online profile.



?

C++的管理擴(kuò)展

簡(jiǎn)介

C++管理擴(kuò)展是一組語(yǔ)言擴(kuò)展,它幫助Microsoft Visual C++開發(fā)人員為微軟.NET編寫應(yīng)用程序。

管理擴(kuò)展是有用的,如果你:

  • 希望提高開發(fā)人員使用C++編寫.NET應(yīng)用程序的生產(chǎn)率
  • 要分階段地將一大段代碼從非管理C++中移植到.NET平臺(tái)上
  • 想從.NET Framework應(yīng)用程序中使用已有的非管理C++組件。
  • 想從非管理C++中使用.NET Framework組件
  • 在同一應(yīng)用程序中混合非管理C++代碼和.NET代碼

C++管理擴(kuò)展為開發(fā)人員定位.NET Framework提供了無(wú)比的靈活性。傳統(tǒng)的非管理C++和管理C++代碼可以自由地混合在一個(gè)
應(yīng)用程序中。用管理擴(kuò)展編寫的應(yīng)用程序可以利用兩種代碼的優(yōu)點(diǎn)。使用管理擴(kuò)展,現(xiàn)有組件可以方便地封裝到.NET組件中,
在與.NET集成的同時(shí)保留原有投資。

什么是管理擴(kuò)展?
擴(kuò)展允許你在C++中編寫在.NET Framework控制下運(yùn)行的管理(或.NET)類。(非管理C++類運(yùn)行在傳統(tǒng)的微軟基于Windows?
的環(huán)境中。)一個(gè)管理類是一個(gè)內(nèi)置的.NET類,可以完全利用.NET Framework。

管理擴(kuò)展是Visual C++開發(fā)系統(tǒng)的新關(guān)鍵字和屬性。它們?cè)试S開發(fā)人員決定哪些類或函數(shù)編譯為管理或非管理代碼。
這些部分然后就可以平滑地與其它部分或外部庫(kù)交互。

管理擴(kuò)展也用于在C++源代碼中表示.NET類型和概念。這就允許開發(fā)人員容易地編寫.NET應(yīng)用程序,而無(wú)需編寫額外代碼。

主要使用環(huán)境

  • 將現(xiàn)有代碼平滑地移植到 .NET
    如果你在C++代碼上有大量投資,管理擴(kuò)展將幫你將它們平滑地轉(zhuǎn)移到.NET平臺(tái)中。因?yàn)槟憧梢栽谝粋€(gè)應(yīng)用程序--
  • 甚至是同一文件中混合管理和非管理代碼,你可以用很長(zhǎng)時(shí)間轉(zhuǎn)移代碼,一個(gè)組件接一個(gè)組件地轉(zhuǎn)換到.NET中。
  • 或你可以繼續(xù)在非管理C++中編寫組件,以利用該語(yǔ)言的強(qiáng)大功能和靈活性,只用管理擴(kuò)展編寫少量的高性能的
  • 封裝器(它使你的代碼可以從.NET組件中調(diào)用)。
  • 從 .NET語(yǔ)言中訪問(wèn)C++組件
    管理擴(kuò)展允許你從任何.NET語(yǔ)言中調(diào)用C++類。你需要用擴(kuò)展編寫簡(jiǎn)單的封裝器,它將你的C++類和方法暴露為
  • 管理類。封裝器是完全的管理類,可以從任何.NET語(yǔ)言中調(diào)用。封裝器類是作為了管理類與非管理C++類間的
  • 映射層。它簡(jiǎn)單地將方法調(diào)用直接傳遞到非管理類中。管理擴(kuò)展可用于調(diào)用任何內(nèi)置的動(dòng)態(tài)鏈接庫(kù)(DLL)及
  • 內(nèi)置類。
  • 從內(nèi)置代碼中訪問(wèn).NET 類
    使用管理擴(kuò)展,你可以創(chuàng)建并從C++代碼中直接調(diào)用.NET類。你可以編寫將.NET組件當(dāng)作任何其它管理C++類的
  • C++代碼。你可以使用.NET Framework中內(nèi)置的COM調(diào)用.NET類。你使用COM還是使用管理擴(kuò)展訪問(wèn).NET組件
  • 要依賴于你的工程。在一些情況下,利用現(xiàn)有的COM支持是最好的選擇。在另一些情況下,使用管理擴(kuò)展可能
  • 會(huì)增加性能和開發(fā)者的生產(chǎn)率。
  • 在同一可執(zhí)行文件中的管理和內(nèi)置代碼
    Visual C++編譯器能在管理和非管理上下文中自動(dòng)而透明的翻譯數(shù)據(jù)、指針和指令流。這個(gè)過(guò)程是允許管理擴(kuò)展
  • 無(wú)縫地與非管理代碼交互的過(guò)程。開發(fā)人員能夠控制什么樣的數(shù)據(jù)和代碼可以管理。選擇每個(gè)類或函數(shù)是管理
  • 還是非管理的能力為開發(fā)人員提供了更大的靈活性。一些代碼或數(shù)據(jù)類型在非管理環(huán)境中執(zhí)行得要比較好。
  • 另一方面,管理代碼由于如碎片收集和類庫(kù)等特性,它提高了開發(fā)人員的生產(chǎn)率。現(xiàn)有非管理代碼可以一次
  • 一部分地轉(zhuǎn)化為管理代碼,因此保留了已有的投資。

posted on 2005-12-29 15:14 夢(mèng)在天涯 閱讀(1024) 評(píng)論(2)  編輯 收藏 引用 所屬分類: Manage c++ /CLI

評(píng)論

# re: A first look at C++/CLI 2006-08-17 17:10 夢(mèng)在天涯

c++\CLI全新的語(yǔ)法,結(jié)合了native c++ 和.net的大部分功能:(VS2005支持c++\CLI,例如建立visual c++下的CLR Console Project)

Elegant syntax and grammar -This gave a natural feel for C++ developers writing managed code and allowed a smooth transition from unmanaged coding to managed coding. All those ugly double underscores are gone now.
First class CLI support - CLI features like properties, garbage collection and generics are supported directly. And what's more, C++/CLI allows jus to use these features on native unmanaged classes too.
First class C++ support - C++ features like templates and deterministic destructors work on both managed and unmanaged classes. In fact C++/CLI is the only .NET language where you can *seemingly* declare a .NET type on the stack or on the native C++ heap.
Bridges the gap between .NET and C++ - C++ programmers won't feel like a fish out of water when they attack the BCL
The executable generated by the C++/CLI compiler is now fully verifiable.
  回復(fù)  更多評(píng)論   

# re: A first look at C++/CLI(C++的管理擴(kuò)展) 2006-08-18 10:14 夢(mèng)在天涯

Managed c++ VS2002支持,

c++\CLI 全新的語(yǔ)法,vs2005支持,

他們不是同一個(gè)東西.  回復(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

搜索

  •  

積分與排名

  • 積分 - 1811983
  • 排名 - 5

最新評(píng)論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              亚洲一区一卡| 亚洲国产91| 久久夜色精品国产| 久久国产毛片| 久久久久久久综合色一本| 亚洲欧美资源在线| 久久一区二区三区超碰国产精品| 久久久www成人免费毛片麻豆| 久久国产精品一区二区| 久久亚洲精品伦理| 欧美日韩免费在线视频| 国产精品国产a| 影视先锋久久| 中文在线资源观看视频网站免费不卡| av成人免费观看| 欧美专区在线观看| 免费在线看成人av| 亚洲午夜未删减在线观看| 久久精品成人一区二区三区| 欧美夫妇交换俱乐部在线观看| 欧美视频日韩| 一区二区在线观看av| 一道本一区二区| 久久久国产精彩视频美女艺术照福利| 免费的成人av| 亚洲一线二线三线久久久| 久久久精品视频成人| 欧美视频中文一区二区三区在线观看 | 亚洲国产精品视频| 午夜精品免费| 欧美黄色成人网| 亚洲欧美日韩中文视频| 欧美jizz19hd性欧美| 国产精品爽黄69| 夜夜嗨av一区二区三区中文字幕 | 国产精品一区久久| 亚洲福利av| 欧美一区2区视频在线观看 | 欧美伊人久久| 欧美电影电视剧在线观看| 一区二区免费在线观看| 欧美aⅴ一区二区三区视频| 国产日韩在线亚洲字幕中文| 亚洲精品在线视频观看| 久久综合伊人| 亚洲欧美日韩成人| 国产精品大片免费观看| 亚洲国产精品一区在线观看不卡| 午夜老司机精品| 日韩亚洲欧美在线观看| 欧美成人精品在线播放| 亚洲国产精品一区二区尤物区| 久久午夜羞羞影院免费观看| 亚洲自拍偷拍视频| 国产精品户外野外| 亚洲免费av片| 欧美国产欧美亚洲国产日韩mv天天看完整 | 久久久综合精品| 国产精品一区二区三区四区| 日韩视频―中文字幕| 欧美国产日本高清在线| 蜜月aⅴ免费一区二区三区 | 麻豆精品精品国产自在97香蕉| 国产日韩欧美在线| 午夜久久资源| 欧美一区二区啪啪| 国产婷婷色一区二区三区| 久久激情五月婷婷| 性色av一区二区三区在线观看| 国产精品一区二区在线观看不卡 | 国产精品一区二区三区四区 | 国产农村妇女精品| 欧美在线视频免费播放| 亚洲综合第一页| 在线视频免费在线观看一区二区| 91久久嫩草影院一区二区| 亚洲资源在线观看| 国产精品青草综合久久久久99 | 亚洲区第一页| 一区二区三区高清| 国产精品亚发布| 老司机午夜精品| 欧美成人中文字幕| 一本大道av伊人久久综合| 在线视频你懂得一区二区三区| 欧美视频一区在线观看| 久久精品视频在线| 欧美成人一区二区三区片免费| 久久蜜桃精品| 中文亚洲视频在线| 欧美亚洲综合在线| 亚洲激情在线| 亚洲一区在线直播| 亚洲激情电影在线| 在线综合+亚洲+欧美中文字幕| 国产欧美日韩三级| 亚洲风情在线资源站| 欧美午夜宅男影院在线观看| 久久九九精品99国产精品| 老色鬼久久亚洲一区二区| 国产精品99久久不卡二区| 亚洲综合激情| 亚洲免费黄色| 久久国产精品黑丝| 亚洲一区二区三区在线| 久久精品观看| 亚洲精品美女久久久久| 亚洲嫩草精品久久| 9色精品在线| 久久精品女人天堂| 亚洲欧美精品在线观看| 欧美国产一区二区在线观看| 欧美一区精品| 欧美三级免费| 亚洲国产第一| 韩日欧美一区二区三区| 99亚洲视频| 亚洲激情在线观看视频免费| 午夜精品国产| 亚洲欧美日韩国产一区二区三区| 久久香蕉国产线看观看av| 欧美在线视频二区| 国产精品v欧美精品v日本精品动漫| 欧美mv日韩mv亚洲| 国产亚洲一区二区三区| 亚洲新中文字幕| 亚洲精品久久久久久久久| 麻豆av福利av久久av| 快射av在线播放一区| 国产欧美精品日韩| 亚洲影视综合| 午夜精品福利在线观看| 欧美日韩日日夜夜| 9l国产精品久久久久麻豆| 99精品国产热久久91蜜凸| 免费欧美高清视频| 欧美激情一区二区久久久| 1000部国产精品成人观看| 羞羞漫画18久久大片| 久久精品伊人| 国产麻豆综合| 欧美在线国产精品| 久久天天躁狠狠躁夜夜爽蜜月| 国产视频久久久久| 欧美亚洲一区二区在线观看| 性色av一区二区三区在线观看 | 久久成人精品电影| 久久男人av资源网站| 国产美女一区| 欧美在线视频全部完| 狠狠噜噜久久| 久久久噜噜噜| 亚洲图片欧美一区| 欧美午夜电影在线观看| 亚洲乱码国产乱码精品精| aa国产精品| 国产精品主播| 亚洲第一在线综合在线| 9色porny自拍视频一区二区| 在线观看福利一区| 国产一区二区在线免费观看| 欧美午夜精品久久久| 免费观看日韩| 麻豆成人精品| 久久久久久黄| 欧美在线视频全部完| 欧美一区二视频在线免费观看| 一本大道久久a久久精品综合| 欧美福利视频| 欧美激情在线有限公司| 免费成人高清视频| 美女999久久久精品视频| 欧美一区二区三区四区高清| 亚洲欧美中日韩| 亚洲欧美日韩国产一区二区三区| 一区二区三区欧美在线观看| 亚洲美女视频网| 一区二区三欧美| 一本色道久久综合一区| 亚洲色诱最新| 一区二区三区|亚洲午夜| 亚洲视频www| 一区二区精品国产| 亚洲网站在线观看| 亚洲欧美网站| 性伦欧美刺激片在线观看| 欧美一区二区啪啪| 久久久久.com| 欧美激情一区二区| 日韩视频中文| 亚洲曰本av电影| 欧美尤物巨大精品爽| 久久伊人精品天天| 欧美福利电影在线观看| 欧美日韩一卡二卡| 国产欧美va欧美va香蕉在| 在线观看视频日韩| 一二三区精品| 久久精品亚洲一区二区三区浴池| 免费h精品视频在线播放|