JavaScript EditorDhtml editor     Free javascript download 



Main Page

The way to declare a reference class type changed from Managed Extensions for C++ to Visual C++ 2005.

In Managed Extensions, a reference class type is prefaced with the __gc keyword. In the new syntax, the __gc keyword is replaced by one of two spaced keywords: ref class or ref struct. The choice of struct or class simply indicates the public (for struct) or private (for class) default access level of its members declared within an initial unlabeled portion of the body of the type.

Similarly, in Managed Extensions, a value class type is prefaced with the __value keyword. In the new syntax, the __value keyword is replaced by one of two spaced keywords: value class or value struct.

An interface type, in Managed Extensions, was indicated with the keyword __interface. In the new syntax, this is replaced with interface class.

For example, the following class declarations in Managed Extensions:

В CopyCode imageCopy Code
public __gc class Block {};     // reference class
public __value class Vector {}; // value class
public __interface IFooBar {};  // interface class

Under the new syntax these are equivalently declared as follows:

В CopyCode imageCopy Code
public ref class Block {};         // reference class
public value class Vector {};      // value class
public interface class IFooBar {}; // interface class

The choice of ref (for reference type) over gc (for garbage collected) is thought to better suggest the fundamental nature of the type.

Specifying the Class as Abstract

Under Managed Extensions, the keyword __abstract is placed before the class keyword (either before or after the __gc) to indicate that the class is incomplete and that objects of the class cannot be created within the program:

В CopyCode imageCopy Code
public __gc __abstract class Shape {};
public __gc __abstract class Shape2D: public Shape {};

Under the new syntax, the abstract contextual keyword is specified following the class name and before either the class body, base class derivation list, or semicolon.

В CopyCode imageCopy Code
public ref class Shape abstract {};
public ref class Shape2D abstract : public Shape{};

Of course, the semantic meaning is unchanged.

Specifying the Class as Sealed

Under Managed Extensions, the keyword __sealed is placed before the class keyword (either before or after __gc) to indicate that objects of the class cannot be inherited from:

В CopyCode imageCopy Code
public __gc __sealed class String {};

Under the new syntax, the sealed contextual keyword is specified following the class name and before either the class body, base class derivation list, or semicolon. (You can both derive a class and seal it. For example, the String class is implicitly derived from Object. The benefit of sealing a class is that it allows the static resolution (that is, at compile-time) of all virtual function calls through the sealed reference class object. This is because the sealed specifier guarantees that the String tracking handle cannot refer to a subsequently derived class that might provide an overriding instance of the virtual method being invoked.

В CopyCode imageCopy Code
public ref class String sealed {};

One can also specify a class as both abstract and sealed – this is a special condition that indicates a static class. This is described in the CLR documentation as follows:

"A type that is both abstract and sealed should have only static members, and serves as what some languages call a namespace."

For example, here is a declaration of an abstract sealed class using the Managed Extensions syntax:

В CopyCode imageCopy Code
public __gc __sealed __abstract class State {
public:
   static State() {}
   static bool inParamList();

private:
   static bool ms_inParam;
};

and here is this declaration translated into the new syntax:

В CopyCode imageCopy Code
public ref class State abstract sealed {
public:
   static State();
   static bool inParamList();

private:
   static bool ms_inParam;
};

CLR Inheritance: Specifying the Base Class

Under the CLR object model, only public single inheritance is supported. However, Managed Extensions retained the ISO-C++ default interpretation of a base class without an access keyword as specifying a private derivation. This meant that each CLR inheritance declaration had to provide the public keyword simply to override the default interpretation. Many users felt this was a bit severe on the compiler's part.

В CopyCode imageCopy Code
// Managed Extensions: error: defaults to private derivation
__gc class foo : bar{};

In the new syntax definition, the absence of an access keyword defaults to a public derivation in a CLR inheritance definition. Thus, the public access keyword is no longer required, but optional. While this does not require any modification of Managed Extensions for C++ code, I list this change here for completeness.

В CopyCode imageCopy Code
// New syntax: ok: defaults to public derivation
ref class foo : bar{};

See Also

Reference

Classes and Structs (Managed)
abstract (C++)
sealed

Concepts

The Managed Types



JavaScript EditorDhtml editor     Free javascript download 
Bitcoin Dice Site