请教大家一个 template 的问题

DeltaCat 2005-01-02 12:06:37
template<class Type>
2 class Queue;
3 /*******定义模板类Queueltem******/
4 template<class Type>
5 class Queueltem
6 {
7 public:
8 Queueltem(const Type &elem):item(elem){}
9 Queueltem(){}
10 private:
11 Type item;
12 Queueltem*nextltem;
13 friend class Queue<Type>
14 }
15 /*****定义模板类Queue*******/
16 template<class Type>
17 class Queue{
18 public:
19 Queue():front(NULL),_____(A)_____{}
20 ~Queue();
21 Type removed();
22 void add(const Type&);
23 bool is_empty()const{return_________(B)__________;}
24 private:
25 Queueltem<Type>*front;
26 Queueltem<Type>*back;
27 };
//模板类Queue的函数成员remove()的实现
29 //从队列头取出一个节点,并返回该节点的值
30 template<class Type>
31 Type Queue<Type>::remove()
32 {
33 Queueltem<Type>*pFront;//指向节点的临时指针
34 Type retVal; //返回值
35 ___(C)________;
36 retVal=front->item;
37 front=front->nextltem;
38 delete pFront;
39 return retVal;
40 }
//模板类Queue的函数成员add()的实现
42 template<class Type>
43 Void Queue<Type>::add(const Type&newltem)
44 {
45 Queueltem<Tupe>*pNew=new Queueltem<Type>;
46 pNew->item=newltem;
47 _________(D)_______;
48 if(front=NULL)
49 front=back=pNew;
50 else
51 {
52 back->nextltem=pNew;
53 __________(E)____________;
54 }
55 }
56
57 template<class Type>
58 Queue<Type>::~Queue()
59 {
60 Queueltem<Type>*p=front,*p;
61 while(p!=NULL)
62 {
63 q=p->nextltem;
64 delete p;
65 p=q;
66 }
67 }

------------------------------------------------------------------------
问题:
------------
问题1:程序中有几处填空,将它们完成

问题2:题中程序第1、2行为什么要说明一下类模板Queue?如果没有这两行语句,程序还正确吗?

问题3:程序第22、23行各有一个const,他们各自表示什么含义?

问题4:程序中模板类Queue的析构函数主要做了什么事情?为什么要这么做?

问题5:下面的程序使用了queue.h文件中定义的类模板,说明程序中哪些定义队列对象的语句是不正确的,哪些是正确的?
-------------------
#include"queue.h"

void main()
{
Queue q1; //1
Queue<int>q2; //2
Queue<int>q3(100); //3
Queue<int>q4[100]; //4
Queue<int>q5=new Queue<int>; //5
//...
delete q5;
}
...全文
100 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
somedummy 2005-01-02
  • 打赏
  • 举报
回复
看起来有点像C++ Primer里面的code嘛!是不是就从那里面找来的?不过这个题目似乎考的和模板没有什么关系吧?
DeltaCat 2005-01-02
  • 打赏
  • 举报
回复
谢谢楼上这位老兄,我不是学 C++的,这个问题是帮朋友问的,他也不学,只是临时用到
kay_zlc 2005-01-02
  • 打赏
  • 举报
回复
1.
A:back(NULL)
B:NULL == front
C:pFront = front
D:pNew->nextItem = NULL
E:back = pNew

2.Forward Declaration

3.
Ln22:can't modify Type's value
Ln23:is_empty() won't modify the class's member data exception that're qualifed with mutable

4.Release resouces

5.
only 2 and 4 are right

My Options:

You should read more C++ books, such as Data Structure and Algorithm,C++ Primer,Inside C++ Object,More/Effective C++,More/Exception C++,Effective STL...

64,681

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧