/*************************B.h*********************************************/

#if !defined(AFX_B_H__21773A49_ECD0_4400_9640_F7C306E01F51__INCLUDED_)
#define AFX_B_H__21773A49_ECD0_4400_9640_F7C306E01F51__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

template <typename T>
class
{
public:
    B();
    virtual ~B();
    void SetData(T nData);
private:
    T m_nData;
};

template<typename T>
B<T>::B()
{
   
}

template<typename T>
B<T>::~B()
{
   
}

template<typename T>
void B<T>::SetData(T nData)
{
    m_nData = nData;
}

//void B<int>::SetData(int nData);

#endif // !defined(AFX_B_H__21773A49_ECD0_4400_9640_F7C306E01F51__INCLUDED_)

/*****************************B.cpp********************************/

#include "stdafx.h"
#include "B.h"

/*
void B<int>::SetData(int nData)
{
    m_nData = nData;
}
*/

 

/**********************main.cpp**************************/

#include "stdafx.h"
#include <iostream.h>
#include "B.h"

//
类模版
template<typename T>
class A
{
    T m_nData;
public:
    void SetData(T nData);
    T GetData() const;
};

template<typename T>
void A<T>::SetData(T nData)
{
    m_nData = nData;
}

template<typename T>
T A<T>::GetData() const
{
    return m_nData;
}

int main(int argc, char* argv[])
{
    //A     -------> A<int>   
    //
模版  -------> (类类型)
    //
类模版-------> 模版类
   
    A<int> thea;
   
    thea.SetData(10);
   
    cout << thea.GetData() << endl;
   
    //
模版的定义放在头文件中
   
    B<B<int> > theB;   //
类型名  B_B_INT
   
    B<int>     theC;
   
    theB.SetData(theC);
   
    return 0;
}

posted on 2010-02-04 22:25  o无尘o  阅读(193)  评论(0编辑  收藏  举报