开发者

Factory pattern and class templates in C++

开发者 https://www.devze.com 2023-01-10 23:12 出处:网络
I have a hierarchy of class templates.At the top of the hierarchy is an abstract base class (interface).I won\'t know which concrete implementation to instantiate until runtime, so it seems like the p

I have a hierarchy of class templates. At the top of the hierarchy is an abstract base class (interface). I won't know which concrete implementation to instantiate until runtime, so it seems like the perfect situation to use the factory pattern. However, virtual member function templates are not allowed in C++.

How can I achieve a legal design similar to the below in C++?

The individual concrete implementations of the IProduct interface as well as the concrete factories will live in different dynamic libraries, one or more开发者_如何学Go of which will be loaded at runtime.

template<class T> class IProduct
{
public:
   virtual void doWork(const T & data) = 0;
};

template<class T> class ProductA : public IProduct<T> {/*...*/};
template<class T> class ProductB : public IProduct<T> {/*...*/};

class IProductFactory
{
public:
   template<class T> virtual IProduct<T> * createProduct() = 0;
};

class ProductAFactory: public IProductFactory
{
public:
   template<class T> virtual IProduct<T> * createProduct()
   {
      return new ProductA<T>;
   }
};

class ProductBFactory: public IProductFactory
{
public:
   template<class T> virtual IProduct<T> * createProduct()
   {
      return new ProductB<T>;
   }
};


Why can't you templatize IProductFactory on T as well? That would get rid of your error, and it's no less general. The client is still going to have to know what T is in order to call thecreateProduct method.

Edit Re: comment

In order to do this, you will need to just create a templatized function to create the factory. So:

template<class T> IProductFactory<T>* getProductFactory();

Now your factory is templatized, the createProduct method is no longer a member template. Not sure what your criteria is for returning a ProductAFactory vs. a ProductBFactory but you will either have to pass in a string to choose, have this be a member function of another class that would make the decision, or have multiple free functions but only expose one version or another to a particular client.


This doesn't need a template. Does that eliminate your problem?

0

精彩评论

暂无评论...
验证码 换一张
取 消