首页 > 代码库 > c++ 类中模版成员函数

c++ 类中模版成员函数

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(){    }