Using the New Language Keywords

   

Several new keywords has been added to the Visual C++ .NET compiler so that it can build .NET Framework applications. The reason for some of these additions is to maintain cross-language interoperability (CLI), a standard that Microsoft defined in order for applications written in separate programming languages to easily and transparently coexist with each other within the .NET Common Language Runtime (CLR).

In order for all the languages to build CLS-compliant applications, there has to be a standard set of language features, which requires additional keywords to be added to each of the .NET languages. For example, the exception handling done in a .NET application is different from what is done in a standard C++ application. The CLR provides try/except/finally functionality, whereas C++ has try/catch. Although the functionality is basically the same, differences do exist. Therefore, new keywords are needed so the compiler can generate the common language calls for .NET.

Table 2.1 shows a list of all these new keywords with a description of each.

Table 2.1. New Keywords for the .NET Framework in VC++ .NET.
Keyword Description
__abstract Declares an abstract class that cannot be instantiated directly. It must first be derived from, and the new class must provide implementations for any pure virtual methods.
__box Creates a copy of a __value class on the CLR heap.
__delegate Declares a reference to a unique method of a managed class. This is typically used for callback functions, where a pointer to a function is required.
__event Declares an event method in a managed class.
__finally Declares a finally block for a try block. The finally block is called all the time before the except block when an exception is caught, and even if an exception is not thrown.
__gc Declares a managed class type.
__identifier Allows a C++ keyword to be used as an identifier. This is useful when you're accessing external classes that may use a C++ keyword as an identifier.
__interface Declares an interface.
__nogc Declares a native C++ class that is not garbage-collected and is allocated on the standard C++ heap. This keyword is not required because the compiler defaults to __nogc if __gc is not specified.
__pin Prevents an object of a managed class from being moved in memory by the CLR during garbage collection.
__property Declares a property member in a managed class.
__sealed Prevents a class declared with the __gc specifier from being a base class or a method from being overridden in a derived class.
__try_cast Performs a cast on a pointer or throws an exception if the cast fails.
__typeof Returns the system type of a class, the value type, and so on.
__value Declares a value type. This is similar to standard structures in C++.

Of the new keywords, the __gc keyword is probably the most noteworthy. By declaring a type with the __gc keyword, you activate the .NET Framework functionality, such as interoperability and garbage collection, for that type. The following code example shows how a class is declared with the __gc keyword:

 __gc class MyClass { private:    int m_nValue; public:    int GetValue() {return m_nValue;} }; 

It is also possible to declare arrays, pointers, and interfaces with the __gc keyword. Doing so specifies that the value works with the managed heap in the .NET Framework. For example, an array declared as shown here creates the array on the .NET heap and its memory is garbage-collected:

 Int32 myarray[] = __gc new Int32[10]; 

When a pointer is declared with the __gc keyword, it is then free to point into the .NET heap. Furthermore, once the pointer is declared, the compiler takes care of initializing the contents of its memory block to zero. Pointers are declared as shown in the following statement:

 Int32 __gc* pMyInt; 

Other keywords that are useful and commonly used are __property, __event, and __finally. As you work through following sections, you will see uses for the other keywords shown in the Table 2.1.


   
Top


Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net