⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 智能指针和迭代器.txt

📁 用什么语言能编制出好的程序
💻 TXT
字号:
//////////////////////////////////////////////////////////////////////
// TLink.h: interface for the TLink class.
//////////////////////////////////////////////////////////////////////

#if !defined(TLINKH)
#define TLINKH

class TNode
{
public:
	TNode(int num):mNum(num),pNext(0) {}
	~TNode() {}
public:
	TNode* GetNext() const			{return pNext;}
	void   SetNext(TNode* pt)		{pNext=pt;}
	int	   GetNum() const			{return mNum;}
	void   SetNum(int num)			{mNum=num;}
private:
	int		mNum;
	TNode*  pNext;
};


class TLink  
{
public:
	TLink():pHead(0){}
	virtual ~TLink();
public:
	void   InsertNode(int num);	
	TNode* Head() const {return pHead;}

	class  SmartPointer;
	friend SmartPointer;
	class  SmartPointer 
	{
	private:
		TLink&  list;
		TNode * pCur;
	public:
		SmartPointer(TLink& lst):list(lst),pCur(lst.Head()) 
			{}
		bool operator++() {
			if (pCur) {pCur=pCur->GetNext(); return true;}
			return false;
		}
		operator bool() 
			{return static_cast<bool>(pCur);}

		TNode* operator->() {return pCur;}
	};

	SmartPointer Begin() 
		{return SmartPointer(*this);}
private:
	TNode*  pHead;
	
};

#endif 



//////////////////////////////////////////////////////////////////////
// TLink.cpp: implementation of the TLink class.
//////////////////////////////////////////////////////////////////////

#include "TLink.h"
TLink::~TLink()
{
	while (pHead) {
		TNode* tmp=pHead;
		pHead=pHead->GetNext();
		delete tmp;
	}
}
void   TLink::InsertNode(int num)
{
	TNode* addOne=new TNode(num);
	addOne->SetNext(pHead);
	pHead=addOne;
}

#include <iostream.h>
#include "TLink.h"


//////////////////////////////////////////////////////////////////////
// myapp.cpp ---main function
//////////////////////////////////////////////////////////////////////
int main()
{
	TLink list;
	list.InsertNode(1);
	list.InsertNode(2);
	list.InsertNode(3);
	list.InsertNode(4);

	TLink::SmartPointer spt=list.Begin();
	while(spt) {
		cout<<spt->GetNum(); 
		++spt;
	}
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -