曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 3251|回复: 0
打印 上一主题 下一主题

在模板中使用嵌套类

[复制链接]

716

主题

734

帖子

2946

积分

超级版主

Rank: 9Rank: 9Rank: 9

积分
2946
跳转到指定楼层
楼主
发表于 2013-12-28 18:54:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
下面的代码实例演示了在类模板中使用嵌套类。

该示例是一个单向链表结构。

queuetp.h
[
[C++] 纯文本查看 复制代码
// 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 为嵌套类
    class Node
    {
    public:
        Item item;
        Node * next;
        Node(const Item & i):item(i), next(0){ }
    };
    Node * front;       // 指向第一个节点
    Node * rear;        // 指向最后一个节点
    int items;          // 当前 Queue 中的 item 数
    const int qsize;    // Queue 中可用的最大 item 
QueueTP(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); // 增加 item 到队列末尾
    bool dequeue(Item &item);       // 移除第一个节点
};

// QueueTP 方法
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)      // 直到队列为空
    {
        temp = front;       // 保存第一个节点的地
front = front->next;// front 指针指向下一个节
delete temp;        // 删除第一个节点
    }
}

// 在队列中添加节点
template <class Item>
bool QueueTP<Item>::enqueue(const Item & item)
{
    if (isfull())
        return false;
    Node * add = new Node(item);    // 创建新节点

    items++;
    if (front == 0)         // 队列是否为空
        front = add;        // 增加第一个节点
    else
        rear->next = add;   // 否则将新增的节点放到队列的末尾
    rear = add;             // rear 指针永远指向最后一个节点
    return true;
}

// 移除第一个节点
template <class Item>
bool QueueTP<Item>::dequeue(Item & item)
{
    if (front == 0)
        return false;
    item = front->item;     // item 为队列中的第一个 item,此处赋值只是为了主程序中 cout 的输出
    items--;
    Node * temp = front;    // 保存第 1 个 item 的位置
    front = front->next;    // 将下一个节点变为第一个
    delete temp;            // 删除第一个节点
    if (items == 0)
        rear = 0;
    return true; 
}

#endif


测试程序 nested.cpp :
[C++] 纯文本查看 复制代码
// 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; 
}	

在上面程序中,Node 是利用通用类型 Item 来定义的,因此如果声明 QueueTP<double> dq; ,那么就会导致 Node 存储的是 double 值;同理,如果声明 QueueTP<char> cq; ,将导致 Node 存储 char 值。这两个 Node 在两个独立的 QueueTP 类中定义,因此不会发生名称冲突,即一个节点的类型为 Queue<double>::Node ,另一个节点类型为 QueueTP<char>::Node 。

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2024-4-30 09:32 , Processed in 0.079450 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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