日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

visual c++ for .net(新语法)

發布時間:2025/7/14 c/c++ 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 visual c++ for .net(新语法) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

一.Basic

  • System::Console::WriteLine調用靜態方法
  • String^ str = " A String!"; ^表明是一個引用類型
  • gcnew 表明創建一個CLR托管對象
  • //========================================================================= // HELLO WORLD //========================================================================= void HelloWorld() {System::Console::WriteLine(" Hello World!"); } //========================================================================= // ASSEMBLY REFERENCES // Assemblies are referenced with the #using directive. The mscorlib.dll // assembly is automatically referenced by the C++ compiler, so it is not // necessary to include that in source code. //========================================================================= // Get access to the diagnostics libraries in the .NET Frameworks #using <System.dll> // All of the code in Frameworks assemblies are in namespaces. Sometimes // it is useful to bring contents of a particular namespace into the // current scope with a using directive. using namespace System::Diagnostics; // This sample has the following using directive in the header file // samples.h so that the contents of the System namespace are available // throughout the code in this project. using namespace System; //========================================================================= // HANDLES //========================================================================= void Handles() {// Handles refer to an object on the garbage collected heapString^ str = " A String!";Object^ obj = str;// Members of a handle variable are accessed via the arrow operatorConsole::WriteLine(obj->ToString()); } //========================================================================= // GCNEW // The gcnew operator creates a new instance of a type on the garbage // collected heap. When all roots to that object are no longer active, // the CLR garbage collector will cleanup the memory to that object. //========================================================================= void GarbageCollection() {Random^ rand = gcnew Random;Console::WriteLine(" Some random numbers: {0}, {1}, {2}",rand->Next(), rand->Next(), rand->Next()); } //========================================================================= // BASICS SAMPLE //========================================================================= void Basics() {Console::WriteLine("=== Beginning of Basic Concepts Sample ===\n");HelloWorld();Handles();GarbageCollection();Console::WriteLine("\n=== End of Basic Concepts Sample ==="); }

    二.Class

    1.聲明一個引用類型(ref),即class

    //========================================================================= // REF CLASSES //========================================================================= ref class R1 { public:R1() { X = 0; }int X;void F() {Console::WriteLine(" Value of X in instance of R1: {0}", X++);} }; void RefClasses() {R1^ r = gcnew R1;r->F();r->F(); }

    2.值類型用value class

    //========================================================================= // VALUE CLASSES //========================================================================= value class V1 { public:int X;void F() {Console::WriteLine(" Value of X in instance of V1: {0}", X++);} }; void ValueClasses() {V1 v;v.F();v.F(); }

    3.聲明接口(interface class)

    //========================================================================= // INTERFACES //========================================================================= interface class I1 {void F(); }; ref class R2 : I1 { public:R2() { X = 0; }int X;virtual void F() {Console::WriteLine(" Value of X in instance of R2 (I1): {0}", X++);} }; void Interfaces() {I1^ i = gcnew R2;i->F();i->F(); }

    4.抽象類(abstract 關鍵字放后面)

    //========================================================================= // ABSTRACT //========================================================================= ref class Animal abstract { public:virtual String^ Habitat() {return "Earth";} }; ref class PolarBear : Animal { public:virtual String^ Habitat() override {return String::Concat("North Pole, ", Animal::Habitat());} }; void Abstract() {Animal^ creature = gcnew PolarBear;Console::WriteLine(" This animal is located at: {0}", creature->Habitat()); }

    5.密封類

    // SEALED //========================================================================= ref class Currency { public:virtual String^ Color() {return "Every Color";} }; ref class Dollar sealed : Currency { public:virtual String^ Color() override {return "Green";} }; void Sealed() {Dollar^ buck = gcnew Dollar;Console::WriteLine(" The color of a dollar is: {0}",buck->Color()); }

    6.literal關鍵字

    A variable (data member) marked as literal in a /clr compilation is the native equivalent of a static const variable.

    ref struct X {literal int i = 4; }; int main() {int value = X::i; }

    7.無限循環

    內部無限循環,這個記錄下

    for(;;) {//... }

    轉載于:https://www.cnblogs.com/Clingingboy/archive/2011/04/12/2013328.html

    總結

    以上是生活随笔為你收集整理的visual c++ for .net(新语法)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。