国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

C++模板類(lèi)的用法

2020-01-26 15:13:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了C++模板類(lèi)的用法,分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

main.h頭文件如下:

復(fù)制代碼 代碼如下:
template <class T> 
class actioncontainer 

public: 
    //構(gòu)造函數(shù) 
    actioncontainer() 
    { 
        m_nRedoPos = 0; 
        m_nUndoPos = 0; 
    } 
    //容器的接口函數(shù) 
    void add(T value); 
    T redo(); 
    T undo(); 
    //容器的屬性 
private: 
    int m_nRedoPos; 
    int m_nUndoPos; 
    const static int ACTION_SIZE=5; 
 
    T m_RedoAction[ACTION_SIZE]; 
    T m_UndoAction[ACTION_SIZE]; 
}; 
 
template<class T> 
void actioncontainer<T>::add(T value) 

    if (m_nUndoPos >= ACTION_SIZE) 
    { 
        //如果容器已潢,剛調(diào)整添加位置 
        m_nUndoPos = ACTION_SIZE - 1; 
        for(int i = 0; i < ACTION_SIZE; i++) 
        { 
            m_UndoAction[i] = m_UndoAction[i+1]; 
        } 
    } 
    m_UndoAction[m_nUndoPos++] = value; 

 
template<class T> 
T actioncontainer<T>::redo() 

    //將恢復(fù)動(dòng)作復(fù)制到撤銷(xiāo)數(shù)組中 
    m_UndoAction[m_nUndoPos++] = m_RedoAction[--m_nRedoPos]; 
 
    //返回恢復(fù)的動(dòng)作 
    return m_RedoAction[m_nRedoPos]; 

 
template<class T> 
T actioncontainer<T>::undo() 

    m_RedoAction[m_nRedoPos++] = m_UndoAction[--m_nUndoPos]; 
 
    return m_UndoAction[m_nUndoPos]; 
}

main.cpp源文件如下:

復(fù)制代碼 代碼如下:
// test_iostream.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 
// 
#include "StdAfx.h" 
#include "main.h" 
using namespace std; 
 
int _tmain(int argc, _TCHAR* argv[]) 

    actioncontainer<int> intaction; 
 
    //向容器中加動(dòng)作 
    intaction.add(1); 
    intaction.add(2); 
    intaction.add(3); 
    intaction.add(4); 
 
    //撤銷(xiāo)上一步動(dòng)作 
    int nUndo = intaction.undo(); 
    nUndo = intaction.undo(); 
 
    //恢復(fù) 
    int nRedo = intaction.redo(); 
    return 0; 
}

希望本文所述對(duì)大家的C++程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 静海县| 九江县| 衡东县| 渭源县| 理塘县| 祁阳县| 紫云| 宁明县| 景东| 宜春市| 磐安县| 屏东县| 榆中县| 新沂市| 华宁县| 山东省| 九江县| 左贡县| 原阳县| 车险| 陆丰市| 绥芬河市| 依安县| 甘孜| 宁波市| 新蔡县| 周至县| 岳西县| 临安市| 积石山| 元江| 梁山县| 凌源市| 建水县| 慈溪市| 桂林市| 富源县| 江口县| 吐鲁番市| 莱阳市| 静安区|