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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM