Udostępnij za pośrednictwem


Explicit Override of an Interface Member

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Explicit Override of an Interface Member.

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

You often want 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:

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:

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 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

Member Declarations within a Class or Interface (C++/CLI)
Explicit Overrides