close

Template class is similar to function template , 

We can use the template to instead of type of variable or method of class.

For example : 

 

using namespace std;
 
template <typename T>
class Storage
{
private:
    T m_tValue;
public:
    Storage(T tValue)
    {
         m_tValue = tValue;
    }
 
    ~Storage()
    {
    }
 
    void Print()
    {
        std::cout << m_tValue << std::endl;;
    }
};

int main()
{
    // Define some storage units
    Storage<int> nValue(5);
    Storage<double> dValue(6.7);
 
    // Print out some values
    nValue.Print();
    dValue.Print();
}


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JerryCheng 的頭像
    JerryCheng

    KwCheng's blog

    JerryCheng 發表在 痞客邦 留言(0) 人氣()