zoukankan      html  css  js  c++  java
  • 类模板

    类模板与原来的函数模板非常像,这里只给出了一个类模板的例子,其它的不多说了。

    #include <iostream>
    using namespace std;

    template
    <class T,class T2> //可含有多个类型,用逗号分开
    class CSamples //类的声明
    {
    public:
    CSamples(
    const T Values[],int count); //函数定义


    CSamples(
    const T& value)
    {
    m_Values[
    0]=value;
    m_free
    =1;
    }

    CSamples()
    {
    m_free
    =0;
    }

    bool Add(const T& value)
    {
    bool OK=m_free<0;
    if(OK)
    m_Values[m_free
    ++]=value;
    return OK;
    }

    T Max()
    const;

    private:
    T m_Values[
    100];
    int m_free;
    };

    //类函数成员外部定义,一定要加上前面的那些
    template<class T,class T2>
    CSamples
    <T,T2>::CSamples(const T Values[],int count)
    {
    m_free
    =count>100?count:100;
    for(int i=0;i<m_free;i++)
    m_Values[i]
    =Values[i];
    }

    template
    <class T,class T2>
    T CSamples
    <T,T2>::Max() const
    {
    T theMax
    =m_free?m_Values[0]:0;

    for(int i=1;i<m_free;i++)
    if(m_Values[i]>theMax)
    theMax
    =m_Values[i];
    return theMax;

    }

    //空的CBox类
    class CBox
    {

    };

    void main()
    {
    //类模板实例化
    CSamples<CBox,double > mysample();
    // mysample().Max();
    };
  • 相关阅读:
    vim python extension
    aws msk
    Install python3
    sns
    inventory
    批量添加监听端口
    template screen
    DNS name
    add jar and proxy repo
    模型诊断论文心得
  • 原文地址:https://www.cnblogs.com/yagzh2000/p/2176259.html
Copyright © 2011-2022 走看看