Explicit interface, multi-state at runtime (Object-Oriented Programming solution)
Explicit interface: We know what it looks like and it is clearly visible in the source code.
Virtual function: provides support for Polymorphism during runtime and calls a function based on the dynamic type.
Templates and generic programming focus on Implicit interfaces and polymorphism during compilation
See the following template function.
[Cpp] emplate <typename T>
Void DoSometing (T & w)
{
If (w. size ()> 10 & w! = SomeNastWidge)
{
T temp (w );
Temp. normalize ();
Temp. swap (w );
}
}
Template <typename T>
Void DoSometing (T & w)
{
If (w. size ()> 10 & w! = SomeNastWidge)
{
T temp (w );
Temp. normalize ();
Temp. swap (w );
}
} 1. The function called by function w is a set of implicit interfaces. From the code alone, we cannot determine the type of w. However, T must support these implicit interfaces.
2. Because the types of parameter T can be varied, in this way, during the compilation period, the specific types of T lead to different functions called by w in the function body. This is the so-called compilation phase polymorphism.
3. polymorphism during compilation and Runtime
Polymorphism during compilation and runtime can be simply understood as: one is to determine which overload function should be called (generally implemented according to the function rename rules of the compiler, which occurs during compilation ), which of the other virtual functions should be bound (implemented through the virtual function table and virtual function pointer, which occurs at runtime and is dynamically determined based on the virtual function pointer in the class ).
4. explicit and implicit Interfaces
The explicit interface is a complete function signature, specifying the function name, parameter type, and return value.
An implicit interface is composed of a series of valid expressions. What do you mean?
For example, an expression:
W. size ()> 10 & w! = SomeNastWidge
Use expressions to specify the capabilities of type parameter T. These are implicit interfaces.
Remember:
1. Both classes and templates support interfaces and Polymorphism
2. for classes, the interface is explicit (explicit ). Polymorphism is implemented through virtual functions at runtime.
3. The template and interface are implicit (implicit) and are based on valid expressions. Polymorphism is achieved through the implementation of the template during the compilation period and the parsing of function overloading.