zoukankan      html  css  js  c++  java
  • 模板(2)

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

  • 相关阅读:
    SQL查询语句 group by后, 字符串合并
    正则表达式对象模型
    C#正则表达式编程(四):正则表达式
    C#正则表达式编程(三):Match类和Group类用法
    C#正则表达式编程(二):Regex类用法
    C#正则表达式编程(一):C#中有关正则的类
    正则表达式中-分组构造
    正则表达式-定位点
    正则表达式-字符类减法
    正则表达式-匹配标点符号
  • 原文地址:https://www.cnblogs.com/w413133157/p/1663988.html
Copyright © 2011-2022 走看看