64,648
社区成员
发帖
与我相关
我的任务
分享
template <class T> class FreeListManager
{
public:
FreeListManager(int s=0);
~FreeListManager();
T* acquire();
void release(T*);
protected:
static list<T*> freeList;
static list<T*> usedList;
// We have size parameters separated from list, since
// size() function of list container is inefficient.
};
include "xxx.h"
template<class T> list<T*>
FreeListMaager<T>::freeList;
template<class T> list<T*>
FreeListManager<T>::usedList;
//there is no T defined
template<class CLASSA> list<CLASSA*>
FreeListManager<CLASSA>::freeList;
template<class CLASSA> list<CLASSA*>
FreeListManager<CLASSA>::usedList;
template<class CLASSB> list<CLASSB*>
FreeListManager<CLASSB>::freeList;
template<class CLASSB> list<CLASSB*>
FreeListManager<CLASSB>::usedList;
list<CLASSA*> FreeListManager<CLASSA>::freeList;
list<CLASSA*> FreeListManager<CLASSA>::usedList;
list<CLASSB*> FreeListManager<CLASSB>::freeList;
list<CLASSB*> FreeListManager<CLASSB>::usedList;
template <class T> class FreeListManager
{
public:
static int counter;
};
template <class T> int FreeListManager<T>::counter=0;
//the following definition will report redefinition error, either
//template <CLASSA> int FreeListManager<CLASSA>::counter=0;
//template <CLASSB> int FreeListManager<CLASSB>::counter=0;
//i thought the complier only can initial the two static member once, and you can set other value later.
FreeListManager<CLASSA>::counter=2
FreeListManager<CLASSB>::counter=3
main()
{
printf("FreeListManager<CLASSA>::counter=%d\n",FreeListManager<CLASSA>::counter);
printf("FreeListManager<CLASSB>::counter=%d\n",FreeListManager<CLASSB>::counter);
}
Dump of assembler code for function _Z41__static_initialization_and_destruction_0ii:
0x0000000000400777 : push %rbp
0x0000000000400778 : mov %rsp,%rbp
0x000000000040077b : sub $0x10,%rsp
0x000000000040077f : mov %edi,-0x4(%rbp)
0x0000000000400782 : mov %esi,-0x8(%rbp)
0x0000000000400785 : cmpl $0x1,-0x4(%rbp)
0x0000000000400789 : jne 0x4007e4 <_Z41__static_initialization_and_destruction_0ii+109>
0x000000000040078b : cmpl $0xffff,-0x8(%rbp)
0x0000000000400792 : jne 0x4007e4 <_Z41__static_initialization_and_destruction_0ii+109>
0x0000000000400794 : mov $0x602060,%edi
0x0000000000400799 : callq 0x400b9a <_ZN15FreeListManagerI6CLASSAEC1Ev>
0x000000000040079e : mov $0x400a1a,%edi
0x00000000004007a3 : mov $0x602040,%edx
0x00000000004007a8 : mov $0x602060,%esi
0x00000000004007ad : callq 0x400630 <__cxa_atexit@plt>
0x00000000004007b2 : mov $0x602068,%eax
0x00000000004007b7 : movzbl (%rax),%eax
0x00000000004007ba : test %al,%al
0x00000000004007bc : jne 0x4007e4 <_Z41__static_initialization_and_destruction_0ii+109>
0x00000000004007be : mov $0x602068,%eax
0x00000000004007c3 : movb $0x1,(%rax)
0x00000000004007c6 : mov $0x602070,%edi
0x00000000004007cb : callq 0x400960 <_ZNSt4listIP6CLASSASaIS1_EEC1Ev>
0x00000000004007d0 : mov $0x400c5c,%edi
0x00000000004007d5 : mov $0x602040,%edx
0x00000000004007da : mov $0x602070,%esi
0x00000000004007df : callq 0x400630 <__cxa_atexit@plt>
0x00000000004007e4 : leaveq
0x00000000004007e5 : retq
End of assembler dump.
xxx.hpp:
#include <list>
using std::list;
template <class T> class FreeListManager
{
public:
FreeListManager();
~FreeListManager(){}
T* acquire();
void release(T*);
// protected:
static list<T*> freeList;
static list<T*> usedList;
// We have size parameters separated from list, since
// size() function of list container is inefficient.
};
template <class T> list<T*> FreeListManager <T>::freeList;
template <class T> FreeListManager<T>::FreeListManager()
{
freeList.push_back(new T);
}
class CLASSA
{
};
class CLASSB
{
};
main.cpp:
#include "xxx.hpp"
typedef FreeListManager<CLASSA> mgr;
int main()
{
mgr aaa;
return 0;
}
#include <cstdlib>
#include <cctype>
#include <complex>
using namespace std;
template<class T>
class Singleton
{
public:
static T *Instance(); // Create an instance of the object
static T &ref_getSingleton(); // Get a reference to the object
static T *ptr_getSingleton(); // Get a pointer to the object
protected:
Singleton(); // notice that the constructor is NOT public
private:
static T *m_instance; // The actual object
};
template<class T>
T *Singleton<T>::m_instance = NULL ;
template<class T>
T *Singleton<T>::Instance()
{
if(m_instance == NULL)
return new Singleton() ;
else
return m_instance ;
}
class A
{} ;
class B
{} ;
int main()
{
Singleton<A> *a = Singleton<A>::Instance() ;
Singleton<B> *b = Singleton<B>::Instance() ;
system("PAUSE") ;
return 0 ;
}