共用方式為


New C++ Language Features 

In Visual C++ 2005 Visual C++ includes new syntax for writing applications to target the common language runtime. This topic presents an overview of the new syntax. The new syntax is not part of ANSI/ISO C++; it is standardized under the Ecma C++/CLI Specification. For more information on the new syntax, see the C++/CLI Specification.

This syntax replaces the previous syntax, known as Managed Extensions for C++. Managed Extensions for C++ syntax is are still available, but as a deprecated feature under /clr:oldSyntax compiler option. To port code using Managed Extensions for C++, see Managed Extensions for C++ Syntax Upgrade Checklist.

The new syntax is in effect by default when you use /clr.

For information on other new features in Visual C++, see

CLR Data Type Keywords

The following table lists new keywords that have been added to Visual C++. Note that some keywords consist of two words separated by white space. These aggregate keywords are considered keywords despite the fact that, used separately, they have different meanings. The word ref, for example, used without class is not a keyword and can be used as a regular identifier. Likewise, by itself class denotes a native class. But, used together, ref class defines a common language runtime (CLR) reference type.

Keyword Context sensitive Purpose Reference
ref class
ref struct

No

Defines a CLR reference class

Classes and Structs (Managed)

value class
value struct

No

Defines a CLR value class

Classes and Structs (Managed)

interface class
interface struct

No

Defines a CLR interface

interface class

enum class
enum struct

No

Defines a CLR enumeration

enum class

property

Yes

Defines a CLR property

property

delegate

Yes

Defines a CLR delegate

delegate

event

Yes

Defines a CLR event

event (C++)

Override Specifiers

The following keywords can be used to qualify override behavior for derivation. The new keyword is not new to C++ but is listed here because of the additional context in which it can be used. Some specifiers are also valid for native programming. For more information, see How to: Declare Override Specifiers in Native Compilations.

Keyword Context Sensitive Purpose Reference
abstract

Yes

Indicates functions or classes are abstract

abstract (C++)

new

No

Indicates that a function is not an override of a base class version

new (new slot in vtable)

override

Yes

Indicates that a method must be an override of a base-class version

override

sealed

Yes

Prevents classes from being used as base classes

sealed

Keywords for Generics

The following keywords have been added to support generic types. For more information, see Generics (C++).

Keyword Context sensitive Purpose
generic

No

Defines a generic type

where

Yes

Specifies the constraints of a generic typef

Miscellaneous New Keywords

The following keywords have been added to Visual C++.

Keyword Context sensitive Purpose Reference
finally

Yes

Indicates default exception handlings behavior

Exception Handling under /clr

for each

No

Enumerate elements of a collection.

for each, in

gcnew

No

Allocates types on the garbage-collected heap

gcnew

initonly

Yes

Indicates a member can only be initialized at declaration or in a static constructor

initonly

literal

Yes

Creates a literal variable

literal (C++)

nullptr

No

Indicates that a handle or pointer does not point at an object

nullptr

Non-Keyword Language Constructs

The following are language constructs that, while not official keywords, are fundamental to .NET programming. These constructs are implemented as templates. Most are defined in the cli Namespace.

Keyword Purpose Reference
array

Type for representing CLR arrays

array

interior_ptr

Points to data inside reference types.

interior_ptr

pin_ptr

Points to CLR reference types to temporarily suppress the garbage collection system

pin_ptr

safe_cast

Determines and executes the optimal casting method for CLR types

safe_cast

typeid

Retrieves a System.Type object describing the given type or object

typeid

New Operators

Two new operators have been added to Visual C++ to support garbage-collected programming.

Operator Purpose Reference
^

Indicates a handle to an object located on the garbage-collected heap

^ (Handle to Object on Managed Heap)

%

Indicates a tracking reference

% (Tracking Reference)

More CLR Constructs

In addition to keywords and language constructs, this reference section includes the following CLR programming constructs.

Topic Description

__identifier

Enables the use of C++ keywords as identifiers

.NET Framework Equivalents to C++ Native Types

Lists the CLR types used in place of C++ integral types

appdomain __declspec modifier

__declspec modifier that mandates static and global variables exist per appdomain

C-Style Casts with /clr

Describes how C-style casts are interpreted

__clrcall calling convention

Indicates the CLR-compliant calling convention

__cplusplus_cli

Predefined Macros

Custom Attributes (C++)

Describes how to define your own CLR attributes.

Exception Handling under /clr

Provides an overview of exception handling

Explicit Overrides

Demonstrates how member functions can override arbitrary members

Friend Assemblies

Discusses how a client assembly can access all types in an assembly component.

Implicit Boxing

Demonstrates the conditions where values types are boxed

Compiler Support for Type Traits

Discusses how to detect characteristics of types at compile time.

managed, unmanaged pragmas

Demonstrates how managed and unmanaged functions can co-exist in the same module

process __declspec modifier

__declspec modifier that mandates static and global variables exist per process

Reflection in C++

Demonstrates the CLR version of run-time type information

System::String Handling in Visual C++

Discusses compiler conversion of string literals to String.

Type Forwarding

Allows you to move a type in a shipping assembly to another assembly, such that, client code does not have to be recompiled.

User-Defined Attributes

Demonstrates use-defined attributes

The #using Directive

Imports external assemblies

XML Documentation (C++)

Explains XML-based code documentation using /doc (Process Documentation Comments) (C/C++)

See Also

Reference

Breaking Changes in the Visual C++ 2005 Compiler

Other Resources

.NET Programming in C++
Native and .NET Interoperability