模板(2)

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

#if !defined(AFX_B_H__<?xml:namespace prefix = st1 />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;
}
*/

<?xml:namespace prefix = o />

 

/**********************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;
}

转载于:https://www.cnblogs.com/w413133157/archive/2010/02/04/1663988.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值