順手翻了下3d游戲引擎設(shè)計(jì)實(shí)時(shí)計(jì)算機(jī)圖形學(xué)的應(yīng)用方法這本書(英文本第二版)
在其800page.18.4章的
template<class T>
class Stack中這兒有一個(gè)小錯(cuò)誤
就是
在函數(shù)
bool Push..
中間 其判斷條件是錯(cuò)誤的
對(duì)于這個(gè)類模板
我把它成了了蓋莫引擎死板的風(fēng)格
如下:
///////////////////////////////////////////////////////////
/// 定義引擎棧模板類
///////////////////////////////////////////////////////////
template<class T>
class Stack : NonCopyable
{
public:
///////////////////////////////////////////////////////
/// 構(gòu)造,析構(gòu)棧模板
///////////////////////////////////////////////////////
Stack(int maxsize);
~Stack();
public:
///////////////////////////////////////////////////////
/// 數(shù)據(jù)入棧
///////////////////////////////////////////////////////
bool Push(const T& element);
///////////////////////////////////////////////////////
/// 數(shù)據(jù)出棧
///////////////////////////////////////////////////////
bool Pop(T& element);
//////////////////////////////////////////////////////////
/// 獲取棧頂元素
//////////////////////////////////////////////////////////
bool GetTop(T& element)const;
///////////////////////////////////////////////////////
/// 檢查棧是否為空棧,滿棧
///////////////////////////////////////////////////////
bool IsEmpty()const;
bool IsFull()const;
private:
int maxsize;
int top;
T* data;
};
#include <GEngine/Template/Stack.inl>
in stack.inl
///////////////////////////////////////////////////////
/// 構(gòu)造,析構(gòu)棧模板
///////////////////////////////////////////////////////
template<class T>
Stack<T>::Stack(int maxsize):
top(-1)
{
ASSERT(maxsize > 0);
this->maxsize = maxsize;
data = new T[this->maxsize];
}
template<class T>
Stack<T>::~Stack()
{
CHECK_PTR_ARRAY_AND_DELETE(data);
}
///////////////////////////////////////////////////////
/// 數(shù)據(jù)入棧
///////////////////////////////////////////////////////
template<class T>
inline bool Stack<T>::Push(const T& element)
{
if(top < maxsize-1)
{
data[++top] = element;
return true;
}
return false;
}
///////////////////////////////////////////////////////
/// 數(shù)據(jù)出棧
///////////////////////////////////////////////////////
template<class T>
inline bool Stack<T>::Pop(T& element)
{
if(top >=0)
{
element = data[top--];
return true;
}
return false;
}
//////////////////////////////////////////////////////////
/// 獲取棧頂元素
//////////////////////////////////////////////////////////
template<class T>
inline bool Stack<T>::GetTop(T& element)const
{
if(top >= 0)
{
element = data[top];
return true;
}
return false;
}
///////////////////////////////////////////////////////
/// 檢查棧是否為空棧,滿棧
///////////////////////////////////////////////////////
template<class T>
inline bool Stack<T>::IsEmpty()const
{
return top == -1;
}
template<class T>
inline bool Stack<T>::IsFull()const
{
return top == maxsize-1;
}