代码改变世界

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

2011-04-12 10:11  Clingingboy  阅读(1043)  评论(0编辑  收藏  举报

 

一.Basic

  1. System::Console::WriteLine调用静态方法
  2. String^ str = " A String!"; ^表明是一个引用类型
  3. 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 heap
    String^ str = "    A String!";
    Object^ obj = str;
    // Members of a handle variable are accessed via the arrow operator
    Console::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(;;) 
{
    //...
}