c++ 类模版、成员函数模版、函数模版 用法

时间:2021-02-26 05:33:20

C++函数模版与类模版。

template <class T>
void SwapFunction(T &first, T &second){ }//函数模版
template <class T>//类模版
class CTemplate{
public:
void SWap(T &first, T &second){ }
};
#include <iostream>
class Single{
public:
static Single* ShareInstance();
static void ReleaseInstance();
template<class Tex>//要成员函数实现模版用法,而类不是模版类。需要写成这个格式
void SwapEx(Tex &obj1, Tex &obj2){
}
private:
Single();
~Single();
Single(const Single &other);
Single& operator=(const Single &other);
private:
static Single *m_pObj;
}; Single* Single::m_pObj = NULL;
Single* Single::ShareInstance(){
if (NULL == m_pObj) {
Single obj;
m_pObj = new Single();
}
return m_pObj;
} void Single::ReleaseInstance(){
if (m_pObj) {
delete m_pObj;
m_pObj = NULL;
}
} Single::Single(){ }
Single::~Single(){ }