C++模板類的用法
更新時(shí)間:2014年10月21日 10:21:20 投稿:shichen2014
這篇文章主要介紹了C++模板類的用法,實(shí)例講述了模板類的概念及相關(guān)用法,需要的朋友可以參考下
本文實(shí)例講述了C++模板類的用法,分享給大家供大家參考。具體實(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ù)制到撤銷數(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];
}
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ù)制到撤銷數(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);
//撤銷上一步動(dòng)作
int nUndo = intaction.undo();
nUndo = intaction.undo();
//恢復(fù)
int nRedo = intaction.redo();
return 0;
}
//
#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);
//撤銷上一步動(dòng)作
int nUndo = intaction.undo();
nUndo = intaction.undo();
//恢復(fù)
int nRedo = intaction.redo();
return 0;
}
希望本文所述對大家的C++程序設(shè)計(jì)有所幫助。
相關(guān)文章
C語言實(shí)現(xiàn)單位車輛調(diào)度管理
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)單位車輛調(diào)度管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03VS中scanf為何會(huì)報(bào)錯(cuò)詳解
在我們剛使用vs時(shí),在使用scanf函數(shù)時(shí)常會(huì)遇到報(bào)錯(cuò)提醒,下面這篇文章主要給大家介紹了關(guān)于VS中scanf為何會(huì)報(bào)錯(cuò)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02在C++中關(guān)于友元函數(shù)的進(jìn)一步理解
今天小編就為大家分享一篇關(guān)于在C++中關(guān)于友元函數(shù)的進(jìn)一步理解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12C/C++?Qt?給ListWidget組件增加右鍵菜單功能
本篇文章給大家介紹ListWidget組件增加一個(gè)右鍵菜單,當(dāng)用戶在ListWidget組件中的任意一個(gè)子項(xiàng)下右鍵,我們讓其彈出這個(gè)菜單,并根據(jù)選擇提供不同的功能,感興趣的朋友跟隨小編一起看看吧2021-11-11MATLAB算法技巧和實(shí)現(xiàn)斐波那契數(shù)列的解決思路
這篇文章主要介紹了MATLAB算法技巧和實(shí)現(xiàn)斐波那契數(shù)列,這篇主要說一下自己在算法設(shè)計(jì)課上用matlab做的兩道算法題,題目解起來都比較簡單,但是需要些技巧,需要的朋友可以參考下2022-12-12