Question

In Alexandrescu's book there is a piece of code that really confuses me.

template <template <class Created> class CreationPolicy> 
class WidgetManager : public CreationPolicy<Widget> 
{ 
 ... 
};

As far as I can tell its a template class within a template. So we accept 'Created' and make it into CreationPolicy class and then publicly inheritting WidgetManager from newly created CreationPolicy class. How is that even possible? Am misinterpreting something here?

Était-ce utile?

La solution

CreationPolicy is a template class, specialised on some class Created.

WidgetManager is derived from a CreationPolicy specialised specifically for creating Widget objects (the thing that the WidgetManager is managing).

A declaration for Widget must already be avaiable to the compiler.

WidgetManager is also a template class, specialised on the CreationPolicy (which is itself a templated class).

What Alexandrescu has done is decouple the manager from the way it creates objects. He has also decoupled the creation means from the object that is being created and managed.

So you could do this:

WidgetManager<MemoryCreator> manager1 = ...;

WidgetManager<FileSystemCreator> manager2 = ...;

and then you can ask manager1 or manager2 for a Widget object without caring how they are created.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top