Veneers has the following features:
1) it is derived from its own main template parameters (usually in the public Mode)
2) It does not define any virtual Methods
3) it does not define any non-static member variables or virtual destructor
4) based on 2) and 3), compared with the template parameter class, it does not increase the Memory Access frequency of the composite class.
Veneers often changes the behavior or type of the template parameter class. The change behavior uses the additional features provided by the sub-template parameters to supplement existing features.
Consider a situation where new features are injected into a very strict hierarchy, and CWindow in ATL is a good example. CWindow does not have a member function that can get the text length of the dialog box. We can use the Win32 API function GetWindowTextLength () and the CWindow member function GetDlgItem () to create a Veneer class parent_window_veneer, the Code is as follows:
Template <typename T>
Classparent_window_veneer: public T
{
Public:
Typedef T ParentClass;
Typedef parent_window_veneer <T> Class;
// Construction
Public:
......
// Operations
Public:
Int GetDlgItemTextLength (UINT id)
{
Return: GetWindowTextLength (GetDlgItem (id ));
}
};
You can use parent_window_veneer <CWindow> to replace the CWindow class. In this way, the GetDlgItemTextLength () method can be used in the instance of this class or its derived class. Of course, this function can be implemented without templated inheritance, but if you want to add this function to another ATL window class (such as CContainedWindow, you have to write another derived class, and so on. Veneer is easy to use by various types of window classes, as long as they contain compatible GetDlgItem () member functions. The only thing left is to modify the type definition (typedef ).
Veneers can also provide destructor, but note that if the template parameter is not a type that can be inherited by polymorphism (for example, it does not contain a virtual destructor ), do not use it in a multi-state manner.
Finally, because Veneers and Their template parameter types are the same, you can use Veneers to replace their template parameter types when an array is required.
Veneers has the following advantages:
A new functional code is successfully injected into the inheritance hierarchy without repeated code writing.
They can be used to add the automatic resource cleaning function to an existing class. RAII is often used. (The container Veneers in the STLSoft Library provides this function for the container class in the C ++ standard library)
The size limit means that the parameterized Veneer type can be used in the array form without encountering the heterogeneous array problem.
The STLSoft library contains some practical examples of the Veneers class, including pod_veneer, conversion_veneer, sequence_container_veneer, and associative_container_veneer. You can view the descriptions of these classes to better understand the concept of Veneers.
Source: http://synesis.com.au/resources/articles/cpp/veneers.pdf
Translation: ume @ 2011-12-03
Keywords: templated inheritance, generic inheritance, templatedinheritance, generic inheritance, Veneers