Interface
Sometimes it is useful to have unrelated classes share a group of public members in order to produce the same behavior. One of the most basic methods may be to define them through a common base class, but this approach is too restrictive because it requires that these classes are interconnected through inheritance, and that they may have their own base classes, and CLI types only support single class inheritance.
C++/CLI provides a way to use multiple classes to implement a common set of functions, which is what we collectively called "Interfaces," while an interface is the declaration of a set of member functions. Note that these functions are just declarations, not defined, that is, an interface that defines a type composed of abstract functions-these functions are actually pure virtual functions and, when appropriate, are implemented by the client class. An interface allows unrelated classes to use the same name and type to implement the same functionality without requiring these classes to share common base classes. In Example 1, we demonstrated how to define an interface.
Example 1:
using namespace System;
public interface class ICollection
{
void Put(Object^ o); //隐式public abstract
Object^ Get(); //隐式public abstract
};
The definition of an interface looks very much like a class, except that with interface instead of ref or value, all functions have no function bodies and are implicitly public and abstract. According to the usual conventions, an interface name with the starting letter I, followed by an uppercase letter. (The interface class is equivalent to the interface structure.) Similar to a class, an interface can also have public or private access to visibility.
An interface can have one or more "base interfaces", in this case, it inherits all the abstract functions in these interfaces, for example, in Example 2, the interface I2 explicitly inherits from the I1, while I3 explicitly inherits from I1 and I2 and inherits from I2 implicitly through I1.
Example 2:
interface class I1 { /* ... */ };
interface class I2 : I1 { /* ... */ };
interface class I3 : I1, I2 { /* ... */ };
A class can implement an interface as it inherits from a base class, as shown in Example 3.
Example 3:
public ref class List : ICollection
{
public:
void Put(Object^ o)
{
// ...
}
Object^ Get()
{
// ...
}
// ...
};
A class can implement more than one interface, in which case a comma must be used to separate the list of interfaces, which is not important in order. Of course, a class can also explicitly carry a base class when implementing one or more interfaces, in which case the base class is usually (but not necessarily) written at the front.
If a class implements an interface, but does not define all the functions in the interface, the class must be declared abstract. Of course, any class that inherits from an abstract class is also an abstract class, unless you define these abstract functions.
interface does not provide multiple inheritance, a CLI class can only have one base class, however, the interface provides some functionality similar to the inheritance of multiple classes, but the concept is completely different, for example, a class cannot inherit a function definition from an interface; The interface inheritance system is independent of the class inheritance system--classes that implement the same interface may , but may not be interconnected through the class inheritance system.
Example 4 illustrates a class: Queue, which has no association with the list (except that, both are inherited from Object), both of which implement the same interface.
Example 4:
public ref class Queue : ICollection
{
public:
void Put(Object^ o)
{
// ...
}
Object^ Get()
{
// ...
}
// ...
};
You can now use it to write a function that handles the parameter list or queue, as in Example 5.
Example 5:
ref class Item { /* ... */ };
void ProcessCollection(ICollection^ c);
int main()
{
List^ myList = gcnew List;
Queue^ myQueue = gcnew Queue;
ProcessCollection(myList);
ProcessCollection(myQueue);
}
void ProcessCollection(ICollection^ c)
{
Item^ x = gcnew Item();
/*1*/ c->Put(x);
/*2*/ x = static_cast<Item^>(c->Get());
}