使用模板类示例

#include "stdio.h"

template <typename T, int SIZE>
class array
{
    T data_[SIZE];
    array (const array& other);
    const array& operator = (const array& other);
public:
    array(){};
    T& operator[](int i) {return data_[i];}
    const T& get_elem (int i) const {return data_[i];}
    void set_elem(int i, const T& value) {data_[i] = value;}
    operator T*() {return data_;}
};            

void main(void)
{
    array<int, 10> intArray;
    int i = 0;
    for (i=0; i<10; i++)
    {
        intArray.set_elem(i, i*10);
    }    
    for (i=0; i<10; i++)
    {
        printf("intArray.get_elem(%d) = %d\n", i, intArray.get_elem(i));
    }
}

 

posted @ 2013-04-15 14:41  Roarsun  阅读(184)  评论(0编辑  收藏  举报