码迷,mamicode.com
首页 > 其他好文 > 详细

模板类

时间:2018-07-06 17:45:45      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:style   pre   []   void   class   模板   template   pen   value   

template <typename T,int size,int value>
class Array { // 在Array类中嵌入了模板类
public:
    Array();
    ~Array();
    void show();
private:
    T * myArray;  // 看这里
};

template <typename T,int size,int value>  // 每个函数前都得有这句话
Array<T, size, value>::Array() {
    myArray = new T[size];
    for (int i = 0; i < size; i++) {
        myArray[i] = value;
    }
}

template <typename T, int size, int value>   // 每个函数前都得有这句话
Array<T,size,value>::~Array() {
    delete[]myArray;
    myArray = NULL;
}

template <typename T,int size,int value>   // 每个函数前都得有这句话
void Array<T, size, value>::show() {
    for (int i = 0; i < size; i++) {
        cout << myArray[i] << endl;
    }
}

 

模板类

标签:style   pre   []   void   class   模板   template   pen   value   

原文地址:https://www.cnblogs.com/pjishu/p/9274375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!