JavaScript EditorDhtml editor     Free javascript download 



Main Page

The syntax for declaring an explicit override of an interface member within a class has changed from Managed Extensions for C++ to Visual C++ 2005.

It is often desirable to provide two instances of an interface member within a class that implements the interface – one that is used when class objects are manipulated through an interface handle, and one that is used when class objects are used through the class interface. For example:

В CopyCode imageCopy Code
public __gc class R : public ICloneable {
   // to be used through ICloneable
   Object* ICloneable::Clone();

   // to be used through an R
   R* Clone();
};

In Managed Extensions we do this by providing an explicit declaration of the interface method with the method's name qualified with the name of the interface. The class-specific instance is unqualified. This eliminates the need to downcast the return value of Clone(), in this example, when explicit called through an instance of R.

In the new syntax, a general overriding mechanism has been introduced that replaces the Managed Extensions syntax. Our example would be rewritten as follows:

В CopyCode imageCopy Code
public ref class R : public ICloneable {
public:
   // to be used through ICloneable
   virtual Object^ InterfaceClone() = ICloneable::Clone;

   // to be used through an R
   virtual R^ Clone();
};

This revision requires that the interface member being explicitly overridden be given a unique name within the class. Here, I've provided the rather awkward name of InterfaceClone(). The behavior is still the same – an invocation through the ICloneable interface invokes the renamed InterfaceClone() while a call through an object of type R invokes the second Clone() instance.

See Also

Reference

Explicit Overrides

Concepts

Member Declarations within a Class or Interface



JavaScript EditorDhtml editor     Free javascript download