主题:单件 模板类静态成员变量重定义问题
求解答。
某DLL模块包含三个文件SimpleTemplate.h、Mgr.h、Mgr.cpp
[b]SimpleTemplate.h[/b]
#ifndef __SimpleTemplate_H_
//!定义一个模板类
template <class T>
class CTemplateTest
{
public:
static int i; //在别的模块中查看其地址与本模块内看到的地址不同,为何?????};
//!初始化静态数据成员
template <class T>
int CTemplateTest<T>::i = 0;
#endif
[b]Mgr.h[/b]
#ifndef _Mgr_H_
#include "SimpleTemplate.h"
#ifdef _Template_SimpleTemplate_Exports
#define Dll_API __declspec(dllexport)
#else
#define Dll_API __declspec(dllimport)
#endif
//!将模板类特化
typedef CTemplateTest<int> CHDBClient;
class Dll_API CHDBClientMgr
{
private:
CHDBClientMgr();
~CHDBClientMgr();
public:
static CHDBClient* Instance();
void Release();
static void set(int i);
static int get();
//!
private:
static CHDBClient* _instance;
};
#endif
[b]Mgr.cpp[/b]
#include "Mgr.h"
#include <iostream>
using namespace std;
CHDBClient* CHDBClientMgr::_instance = NULL;
CHDBClientMgr::CHDBClientMgr()
{
}
CHDBClientMgr::~CHDBClientMgr()
{
}
CHDBClient* CHDBClientMgr::Instance()
{
if (0 == _instance)
{
_instance = new CHDBClient();
}
cout<<"CHDBClientMgr::Instance: 值 CHDBClient::i "<<CHDBClient::i<<endl;
cout<<"CHDBClientMgr::Instance: 地址 &CHDBClient::i "<<&CHDBClient::i<<endl;
cout<<"CHDBClientMgr::Instance: 值 _instance->i: "<<_instance->i<<endl;
cout<<"CHDBClientMgr::Instance: 地址 &_instance->i: "<<&_instance->i<<endl;
return _instance;
}
void CHDBClientMgr::Release()
{
if (_instance)
{
delete _instance;
_instance = NULL;
}
}
void CHDBClientMgr::set(int i)
{
_instance->i = i;
cout<<"set地址1 "<<&_instance->i<<endl;
cout<<"set地址2 "<<&CHDBClient::i<<endl;
}
int CHDBClientMgr::get()
{
cout<<"get地址 &CHDBClient::i "<<&CHDBClient::i<<endl;
cout<<"get值 CHDBClient::i "<<CHDBClient::i<<endl;
cout<<"get地址 "<<&_instance->i<<endl;
cout<<"get值 "<<_instance->i<<endl;
return _instance->i;
}
[b]exe程序输出静态成员变量的地址,居然不唯一,难道静态成员变量被重定义了?????[/b]
#include <iostream>
#include"Mgr.h"
using namespace std;
int main( int argc,char** argv )
{
cout<<"单件类使用了模板类型的静态变量,存在问题"<<endl;
//!被调用模块内
CHDBClientMgr::set(1);
cout<<"CHDBClientMgr::set(1)"<<endl;
CHDBClient* pHdbClient = CHDBClientMgr::Instance();
cout<<"main: CHDBClientMgr::get() "<<endl;
cout<<CHDBClientMgr::get()<<endl;
cout<<endl;
//!本地模块内
cout<<"main: CHDBClient::i "<<CHDBClient::i<<endl;
cout<<"main: &CHDBClient::i "<<&CHDBClient::i<<endl;
cout<<"main: pHdbClient->i: "<<pHdbClient->i<<endl;
cout<<"main: &pHdbClient->i "<<&(pHdbClient->i)<<endl;
cout<<"main: CHDBClientMgr::get() "<<endl;
cout<<CHDBClientMgr::get()<<endl;
cout<<endl;
//!本地模块内
CHDBClientMgr::set(2);
cout<<"CHDBClientMgr::set(2)"<<endl;
cout<<"main: CHDBClient::i "<<CHDBClient::i<<endl;
cout<<"main: &CHDBClient::i "<<&CHDBClient::i<<endl;
cout<<"main: pHdbClient->i: "<<pHdbClient->i<<endl;
cout<<"main: &pHdbClient->i "<<&(pHdbClient->i)<<endl;
cout<<"main: CHDBClientMgr::get() "<<endl;
cout<<CHDBClientMgr::get()<<endl;
cout<<endl;
//!被调用模块内
CHDBClientMgr::Instance();
cout<<"main: CHDBClientMgr::get() "<<endl;
cout<<CHDBClientMgr::get()<<endl;
system("pause");
return 0;
}
某DLL模块包含三个文件SimpleTemplate.h、Mgr.h、Mgr.cpp
[b]SimpleTemplate.h[/b]
#ifndef __SimpleTemplate_H_
//!定义一个模板类
template <class T>
class CTemplateTest
{
public:
static int i; //在别的模块中查看其地址与本模块内看到的地址不同,为何?????};
//!初始化静态数据成员
template <class T>
int CTemplateTest<T>::i = 0;
#endif
[b]Mgr.h[/b]
#ifndef _Mgr_H_
#include "SimpleTemplate.h"
#ifdef _Template_SimpleTemplate_Exports
#define Dll_API __declspec(dllexport)
#else
#define Dll_API __declspec(dllimport)
#endif
//!将模板类特化
typedef CTemplateTest<int> CHDBClient;
class Dll_API CHDBClientMgr
{
private:
CHDBClientMgr();
~CHDBClientMgr();
public:
static CHDBClient* Instance();
void Release();
static void set(int i);
static int get();
//!
private:
static CHDBClient* _instance;
};
#endif
[b]Mgr.cpp[/b]
#include "Mgr.h"
#include <iostream>
using namespace std;
CHDBClient* CHDBClientMgr::_instance = NULL;
CHDBClientMgr::CHDBClientMgr()
{
}
CHDBClientMgr::~CHDBClientMgr()
{
}
CHDBClient* CHDBClientMgr::Instance()
{
if (0 == _instance)
{
_instance = new CHDBClient();
}
cout<<"CHDBClientMgr::Instance: 值 CHDBClient::i "<<CHDBClient::i<<endl;
cout<<"CHDBClientMgr::Instance: 地址 &CHDBClient::i "<<&CHDBClient::i<<endl;
cout<<"CHDBClientMgr::Instance: 值 _instance->i: "<<_instance->i<<endl;
cout<<"CHDBClientMgr::Instance: 地址 &_instance->i: "<<&_instance->i<<endl;
return _instance;
}
void CHDBClientMgr::Release()
{
if (_instance)
{
delete _instance;
_instance = NULL;
}
}
void CHDBClientMgr::set(int i)
{
_instance->i = i;
cout<<"set地址1 "<<&_instance->i<<endl;
cout<<"set地址2 "<<&CHDBClient::i<<endl;
}
int CHDBClientMgr::get()
{
cout<<"get地址 &CHDBClient::i "<<&CHDBClient::i<<endl;
cout<<"get值 CHDBClient::i "<<CHDBClient::i<<endl;
cout<<"get地址 "<<&_instance->i<<endl;
cout<<"get值 "<<_instance->i<<endl;
return _instance->i;
}
[b]exe程序输出静态成员变量的地址,居然不唯一,难道静态成员变量被重定义了?????[/b]
#include <iostream>
#include"Mgr.h"
using namespace std;
int main( int argc,char** argv )
{
cout<<"单件类使用了模板类型的静态变量,存在问题"<<endl;
//!被调用模块内
CHDBClientMgr::set(1);
cout<<"CHDBClientMgr::set(1)"<<endl;
CHDBClient* pHdbClient = CHDBClientMgr::Instance();
cout<<"main: CHDBClientMgr::get() "<<endl;
cout<<CHDBClientMgr::get()<<endl;
cout<<endl;
//!本地模块内
cout<<"main: CHDBClient::i "<<CHDBClient::i<<endl;
cout<<"main: &CHDBClient::i "<<&CHDBClient::i<<endl;
cout<<"main: pHdbClient->i: "<<pHdbClient->i<<endl;
cout<<"main: &pHdbClient->i "<<&(pHdbClient->i)<<endl;
cout<<"main: CHDBClientMgr::get() "<<endl;
cout<<CHDBClientMgr::get()<<endl;
cout<<endl;
//!本地模块内
CHDBClientMgr::set(2);
cout<<"CHDBClientMgr::set(2)"<<endl;
cout<<"main: CHDBClient::i "<<CHDBClient::i<<endl;
cout<<"main: &CHDBClient::i "<<&CHDBClient::i<<endl;
cout<<"main: pHdbClient->i: "<<pHdbClient->i<<endl;
cout<<"main: &pHdbClient->i "<<&(pHdbClient->i)<<endl;
cout<<"main: CHDBClientMgr::get() "<<endl;
cout<<CHDBClientMgr::get()<<endl;
cout<<endl;
//!被调用模块内
CHDBClientMgr::Instance();
cout<<"main: CHDBClientMgr::get() "<<endl;
cout<<CHDBClientMgr::get()<<endl;
system("pause");
return 0;
}