class WidgetManager : public CreationPolicy<Widget>
{
...
};
And now we can use our host class like this:
1
2
3
4
// Application Code
typedef WidgetManager<OpNewCreator> MyWidgetMgr;
// Or
using MyWidgetMgr = WidgetManager<OpNewCreator>;
Add Extra Function To Host Class
1
2
3
4
5
6
7
8
9
10
11
12
// Library Code
template <template <class> class CreationPolicy>
class WdigetManager : public CreationPolicy<Widget>
{
...
voidSwitchPrototype(Widget* pNewPrototype)
{
CreationPolicy<Widget>& myPolicy = *this;
delete myPolicy.GetPrototype();
myPolicy.SetPrototype(pNewPrototype);
}
};
The resulting context is:
If the user instantiates WidgetManager with a Creator policy class that supports prototypes, she can use SwitchPrototype.
If the user instantiates WidgetManager with a Creator policy class that does not support prototypes and tries to use SwitchPrototype, a compile-time error occurs.
If the user instantiates WidgetManager with a Creator policy class that does not support prototypes and does not try to use SwitchPrototype, the program is valid.