C++之类模板

【类模板】

template<class T>
class MyArray{
public:
    void display();//若是类内定义则没什么特别的:void display(){...}
private:
    T *m_pArr;
};
//实现
template<class T>
void MyArry<T>::display(){
...
}
//使用
MyArray<int> arr;
arr.display();
【多参数的类模板】

template <typename T,int KSize>
class Container{
public:
    void display();
private:
    T m_obj;
}
//实现
template <typename T,int KSize>
void Container<T,KSize>::display(){
    for(int i=0;i<KSize;i++){
        cout << m_obj << endl;
    }
}
//使用
Container<int,10> ct1;
ct1.display();
【模板代码不能分离编译,必须都写在.h文件中】
无欲则刚 关心则乱
原文地址:https://www.cnblogs.com/xjyxp/p/11242668.html