看流星社区

 找回密码
 注册账号
查看: 1849|回复: 0

模板类的使用

[复制链接]

该用户从未签到

发表于 2017-6-2 11:08:00 | 显示全部楼层 |阅读模式
main.h
template <class T>
class actioncontainer
{
public:
        //构造函数
        actioncontainer()
        {
                m_nRedoPos = 0;
                m_nUndoPos = 0;
        }
        //容器的接口函数
        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)
        {
                //如果容器已潢,刚调整添加位置
                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()
{
        //将恢复动作复制到撤销数组中
        m_UndoAction[m_nUndoPos++] = m_RedoAction[--m_nRedoPos];

        //返回恢复的动作
        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
// test_iostream.cpp : 定义控制台应用程序的入口点。
//
#include "StdAfx.h"
#include "main.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
        actioncontainer<int> intaction;

        //向容器中加动作
        intaction.add(1);
        intaction.add(2);
        intaction.add(3);
        intaction.add(4);

        //撤销上一步动作
        int nUndo = intaction.undo();
        nUndo = intaction.undo();

        //恢复
        int nRedo = intaction.redo();
        return 0;
}
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-3-19 13:54

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表