【请问】继承了单例模式类模板的工厂模式类模板,如何一次性初始化所有模板实例的成员静态指针

【请教】继承了单例模式类模板的工厂模式类模板,怎么一次性初始化所有模板实例的成员静态指针
我下写了一个单例的工厂类Factory
#ifndef _Factory_H_
#define _Factory_H_

#include <iostream>
#include "Product.cpp"

using namespace std;
//对不同的产品产生不同的工厂(本身就是一个单例)
template<class T>
class Factory
{
public:
//获取作为成员变量的静态单例的函数
static inline Factory* getinstance()
{
if(!_instance)
_instance = new Factory<T>;
return _instance;
}
T* create()
{
return new T;
}
private:
//作为私有成员的构造函数
Factory()
{

}
//作为私有成员的析构函数
~Factory()
{

}
//作为成员变量的静态单例
static Factory* _instance;
};

//注意::可以使用模板为每个模板类的工厂的静态指针_instance初始化
template<class _T>  Factory<_T>* Factory<_T>::_instance = NULL;
#endif

这个Factory工厂不用为每一个产品(ProductA,ProductB)对应的工厂分别初始化静态指针,而是可以用template来一次性为所有模板实例化的工厂初始化静态指针

然后写了一个单例模板类Singleton,用别的类继承这个单例模板来新建单例更加便捷,因为我的程序里要有很多个单例,所以我倾向于用这种方法产生出来的下面的单例工厂FactoryNew(比较好管理),而不是上面的单独写出来的单例工厂Factory
#ifndef __Singleton_H__  
#define __Singleton_H__  

//T是任何继承此单例的类名
template <class T>
class Singleton  
{  
public:  
    //获取类的单例
    static inline T* getinstance();  
    //释放类的单例
    void release();  
protected:
//作为保护成员的构造函数
    Singleton(void){}
//作为保护成员的析构函数
    ~Singleton(void){}
//作为成员变量的静态单例
static T* _instance;
};  

//获取类的单例
template <class T>  
inline T* Singleton<T>::getinstance()  
{  
    if(!_instance)  
        _instance=new T;  
    return _instance;  
}  

//释放类的单例
template <class T>  
void Singleton<T>::release()  
{  
    if(!_instance)  
        return;  
    delete _instance;
    _instance=0;
}  
  
//定义一个宏帮助继承此单例模板类的子类的静态指针_instance初始化
#define DECLARE_SINGLETON_MEMBER(_Ty)\
_Ty* Singleton<_Ty>::_instance=NULL
  
#endif//__Singleton_H__  


新写了一个工厂类FactoryNew,继承单例模板Singleton
#ifndef __FactoryNew_H__  
#define __FactoryNew_H__  


#include "Singleton.h"

//对不同的产品产生不同的工厂类
template <class Pro>
class FactoryNew:public Singleton<FactoryNew<Pro>> //继承自单例类Singleton的工厂模式的模板
{
friend class Singleton<FactoryNew<Pro>>;
public:
Pro *create();

private:
//作为私有成员的构造函数
FactoryNew();
//作为私有成员的析构函数
    ~FactoryNew();  
protected:  

};  
#endif  


#include "FactoryNew.h"

template <class Pro>
//构造函数的定义
FactoryNew<Pro>::FactoryNew()
{
}

//析构函数的定义
template <class Pro>
FactoryNew<Pro>::~FactoryNew()
{

}

template <class Pro>
Pro* FactoryNew<Pro>::create()
{
return new Pro;
}


//问题在此::为什么不能用如下方法一次性给每个模板类的工厂的静态指针初始化
/*
** #define DECLARE_FACTORY_OF(_Pro)\
** FactoryNew<_Pro>* Singleton<FactoryNew<_Pro>>::_instance=NULL
** template<class _Pro> DECLARE_FACTORY_OF(_Pro);
*/

但这个FactoryNew就不能用template方法为一次性为所有工厂的模板实例初始化静态指针

对于产品类ProductA,ProductB,ProductC,ProductD
#ifndef _Product_H_
#define _Product_H_

#include <iostream>

using namespace std;

//产品A,由工厂Factory生产
class ProductA
{
public:
ProductA(){}
~ProductA(){}
void put(){cout<<"productA is here"<<endl;}
};

//产品B,由工厂Factory生产
class ProductB
{
public:
ProductB(){}
~ProductB(){}
void put(){cout<<"ProductB is here"<<endl;}
};

//产品C,由工厂FactoryNew生产
class ProductC
{
public:
ProductC(){}
~ProductC(){}
void put(){cout<<"ProductC is here"<<endl;}
};

//产品D,由工厂FactoryNew生产
class ProductD
{
public:
ProductD(){}
~ProductD(){}
void put(){cout<<"productD is here"<<endl;}
};
#endif


main.h如下
#include <iostream>
#include "Factory.cpp"
#include "Singleton.h"
#include "FactoryNew.cpp"

using namespace std;

//必须分别对每个模板类的工厂的静态指针初始化
DECLARE_SINGLETON_MEMBER(FactoryNew<ProductC>);
DECLARE_SINGLETON_MEMBER(FactoryNew<ProductD>);

int main()
{
/////////////////////////////////////////////////////////////////////////////////
//本身就是单例的工厂模式模板Factory
ProductA *proA = Factory<ProductA>::getinstance()->create();
proA->put();
ProductB *proB = Factory<ProductB>::getinstance()->create();
proB->put();

/////////////////////////////////////////////////////////////////////////////////
//继承自单例类Singleton的工厂模式模板FactoryNew
ProductC *proC = FactoryNew<ProductC>::getinstance()->create();
proC->put();
ProductD *proD = FactoryNew<ProductD>::getinstance()->create();
proD->put();

return 0;
}

FactoryNew必须如main.h中这样分别初始化每个实例的静态指针

如果FactoryNew也用上面注释掉的方法初始化静态指针就会报错:
1>  main.cpp
1>e:\workspace\visual studio\c++\factorytemplate\factorytemplate\factorynew.cpp(38): error C3860: 模板 参数列表(在类 模板 名称后面)必须按 模板 参数列表中使用的顺序列出参数
1>  FactoryNew.cpp
1>e:\workspace\visual studio\c++\factorytemplate\factorytemplate\factorynew.cpp(38): error C3860: 模板 参数列表(在类 模板 名称后面)必须按 模板 参数列表中使用的顺序列出参数
请教,为什么FactoryNew不能像Factory一样用类似方法初始化静态指针,另外FactoryNew有没有简单的方法来初始化静态指针,而不用为每个模板实例都初始化一遍?
------解决思路----------------------
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

请把factorynew.cpp的内容移到factorynew.h中。 


谢谢!

我尝试之后的结果是仍然有报错:
1>  main.cpp
1>e:\workspace\visual studio\c++\factorytemplate\factorytemplate\factorynew.h(54): error C3860: 模板 参数列表(在类 模板 名称后面)必须按 模板 参数列表中使用的顺序列出参数

54行附近的代码是啥?


是这句:
template<class _Pro> DECLARE_FACTORY_OF(_Pro);
没放到一起时报错也是这句

我试了一下没有问题。 


谢谢!
我按你说的,合并了.h和.cpp文件,然后在Singleton.h后面添加这一句:
template<class _Ty> DECLARE_SINGLETON_MEMBER(_Ty);

就没有报错了。

然后,我还有疑问,为什么将.h和.cpp文件合并后就不会出错?

模板的定义一般都放在头文件中的,便于模板实例化。