c++ primer plus 第15章友,异常和其他:15.2.2模板中的嵌套
15.2.2模板中的嵌套
文章目录
- c++ primer plus 第15章友,异常和其他:15.2.2模板中的嵌套
- 15.2.2模板中的嵌套
- 程序清单15.5 queuetp.h
- 程序清单15.6 nested.cpp
15.2.2模板中的嵌套
您知道,模板很适合用于实现诸如 Queue 等容器类。您可能会问,将Queue 类定义转换为模板时,是否会由于它包含嵌套类而带来问题?答案是不会。程序清单 15.5 演示了如何进行这种转换。和类模板一样,该头文件也包含类模板和方法函数模板。
程序清单15.5 queuetp.h
// queuetp.h -- queue template with a nested class
#ifndef QUEUETP_H_
#define QUEUETP_H_template <class Item>
class QueueTP
{
private:enum {Q_SIZE = 10};// Node is a nested class definitionclass Node{public:Item item;Node * next;Node(const Item & i):item(i), next(0){ }};Node * front; // pointer to front of QueueNode * rear; // pointer to rear of Queueint items; // current number of items in Queueconst int qsize; // maximum number of items in QueueQueueTP(const QueueTP & q) : qsize(0) {}QueueTP & operator=(const QueueTP & q) { return *this; }
public:QueueTP(int qs = Q_SIZE);~QueueTP();bool isempty() const{return items == 0;}bool isfull() const{return items == qsize;}int queuecount() const{return items;}bool enqueue(const Item &item); // add item to endbool dequeue(Item &item); // remove item from front
};// QueueTP methods
template <class Item>
QueueTP<Item>::QueueTP(int qs) : qsize(qs)
{front = rear = 0;items = 0;
}template <class Item>
QueueTP<Item>::~QueueTP()
{Node * temp;while (front != 0) // while queue is not yet empty{temp = front; // save address of front itemfront = front->next;// reset pointer to next itemdelete temp; // delete former front}
}// Add item to queue
template <class Item>
bool QueueTP<Item>::enqueue(const Item & item)
{if (isfull())return false;Node * add = new Node(item); // create node
// on failure, new throws std::bad_alloc exceptionitems++;if (front == 0) // if queue is empty,front = add; // place item at frontelserear->next = add; // else place at rearrear = add; // have rear point to new nodereturn true;
}// Place front item into item variable and remove from queue
template <class Item>
bool QueueTP<Item>::dequeue(Item & item)
{if (front == 0)return false;item = front->item; // set item to first item in queueitems--;Node * temp = front; // save location of first itemfront = front->next; // reset front to next itemdelete temp; // delete former first itemif (items == 0)rear = 0;return true;
}#endif
程序清单 15.5中模板有趣的一点是,Node 是利用通用类型 Item 来定义的。所以,下面的声明将导致Node 被定义成用于存储 double 值:
QueueTpdg;
而下面的声明将导致 Node 被定义成用于存储 char 值:
QueueTpcq;
这两个 Node 类将在两个独立的 QueueTP 类中定义,因此不会发生名称冲突。即一个节点的类型为QueueTP::Node,另一个节点的类型为 QueueTP::Node。程序清单15.6是一个小程序,可用于测试这个新的类。
程序清单15.6 nested.cpp
// nested.cpp -- using a queue that has a nested class
#include <iostream>#include <string>
#include "queuetp.h"int main()
{using std::string;using std::cin;using std::cout;QueueTP<string> cs(5);string temp;while(!cs.isfull()){cout << "Please enter your name. You will be ""served in the order of arrival.\n""name: ";getline(cin, temp);cs.enqueue(temp);}cout << "The queue is full. Processing begins!\n";while (!cs.isempty()){cs.dequeue(temp);cout << "Now processing " << temp << "...\n";}// cin.get();return 0;
}