惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Schneier on Security
Schneier on Security
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
A
About on SuperTechFans
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
V2EX - 技术
V2EX - 技术
N
Netflix TechBlog - Medium
F
Fortinet All Blogs
V
Visual Studio Blog
Martin Fowler
Martin Fowler
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
Scott Helme
Scott Helme
H
Heimdal Security Blog
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V
Vulnerabilities – Threatpost
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
Jina AI
Jina AI
S
Securelist
小众软件
小众软件
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
博客园 - 三生石上(FineUI控件)
量子位

博客园 - 玄驹子

设计一个检查控件布局的工具 低级失误害死人 Dogfood ETA 要开始研究.NET了 刚看《孙悟空是一个好员工》,有一段比喻很有意思 我是十足一个大蠢驴 辞职·无奈 辞职·焦虑 辞职·麻烦 等待的滋味最不好受 几个有寓意的小故事 今天老板突然找到我 XML SOAP应用简介 终于出差回来啦 我一天的颓废生活! 软件怎样推动下一代的微处理器 如果领导都觉得自己委屈了 为什么 ClearCase & ClearQuest UCM 不适用于小公司
我写的第一个模板类
玄驹子 · 2004-11-12 · via 博客园 - 玄驹子

    很简单的一个列表类,而且并不是很完善,仅限于在自己的程序中使用。有很多很基本的东西还没搞懂。不过我一直奇怪的是:我将这个类的声明和实现放在不同的文件中时(.h, .cpp),最后在引用用这个类的地方都会出现连接错误,无奈只能将声明和实现放在同一个文件中了(.h),模板类的编译和连接机制到底是怎样的呢?

#pragma once

template
<typename NodeType>
class TQueue;

template 
<typename NodeType>
class QNode
{
public:
    NodeType 
* node;
    QNode
<NodeType> * nextQNode;
    QNode
<NodeType> * prevQNode;
    friend 
class TQueue<NodeType>;
}
;

template 
<typename NodeType>
class TQueue  
{
public:
    TQueue(
void);
    
~TQueue(void);

    WORD GetCount();

    
// Queue Behavior
    NodeType * PickHead();
    
void AppendTail(NodeType * _newNode);
    BOOL IsEmpty();

    
// Sequence Visit
    NodeType * GetNextNode(HANDLE & hcur);
    NodeType 
* PickCurrentNode(HANDLE & hcur);

private:
    WORD _queueLen;
    QNode
<NodeType> * _queueHead, * _queueTail;
}
;


/// --- imp --- ///
template <typename NodeType>
TQueue
<NodeType>::TQueue(void)
{
    _queueLen 
= 0;
    _queueHead 
= NULL;
    _queueTail 
= NULL;
}


template 
<typename NodeType>
TQueue
<NodeType>::~TQueue(void)
{
    NodeType 
* tmpNode = NULL;
    
while(_queueLen>0)
    
{
        tmpNode 
= PickHead();
        delete tmpNode;
    }

}


template 
<typename NodeType> 
WORD TQueue 
<NodeType>::GetCount()
{
    
return _queueLen;
}


// Queue Behavior
template <typename NodeType>
NodeType 
* TQueue<NodeType>::PickHead()
{
    
if(_queueLen == 0)return NULL;
    assert(_queueHead 
!= NULL);

    QNode
<NodeType> * tmpQNode = _queueHead;
    NodeType 
* tmpNode = NULL;

    
if(_queueHead->nextQNode != NULL) // has node lest
    {
        _queueHead
->nextQNode->prevQNode = NULL;
    }

    
else _queueTail = NULL; // no nodes left, so set _queueTail = NULL;
    _queueHead = _queueHead->nextQNode;

    _queueLen
--;
    
    tmpNode 
= tmpQNode->node;
    delete tmpQNode;
    
return tmpNode;
}


template 
<typename NodeType>
void TQueue<NodeType>::AppendTail(NodeType * _newNode)
{
    QNode
<NodeType> * newQNode = new QNode<NodeType>;
    newQNode
->node = _newNode;
    newQNode
->nextQNode = NULL;
    
if(_queueLen == 0// Empty Queue
    {
        newQNode
->prevQNode = NULL;
        _queueHead 
= newQNode;
        _queueTail 
= newQNode;
        _queueLen 
++;
        
return;
    }

    
// Queue not Empty
    assert(_queueTail != NULL);
    _queueTail
->nextQNode = newQNode;
    newQNode
->prevQNode = _queueTail;
    _queueTail 
= newQNode;
    _queueLen 
++;
}


template 
<typename NodeType>
BOOL TQueue
<NodeType>::IsEmpty()
{
    
return (_queueLen==0);
}


// Sequence Visit
template <typename NodeType>
NodeType 
* TQueue<NodeType>::GetNextNode(HANDLE & hcur)
{
    
if (hcur == 0x0000)
    
{
        hcur 
= (HANDLE)_queueHead;
        
if(_queueHead != NULL) return _queueHead->node;
        
else return NULL;
    }


    QNode
<NodeType> * tmpQNode = (QNode<NodeType>*)hcur;
    
if(tmpQNode->nextQNode == NULL)
    
{
        hcur 
= 0x0000;
        
return NULL;
    }

    
else
    
{
        hcur 
= (HANDLE)tmpQNode->nextQNode;
        
return (tmpQNode->nextQNode->node);
    }

}


template 
<typename NodeType>
NodeType 
* TQueue<NodeType>::PickCurrentNode(HANDLE & hcur)
{
    assert(hcur 
!= NULL);
    assert(_queueLen 
> 0);

    QNode
<NodeType> * tmpQNode = (QNode<NodeType>*)hcur;
    NodeType 
* tmpNode = NULL;

    
if (tmpQNode->prevQNode == NULL) // is the head
        _queueHead = tmpQNode->nextQNode;
    
else
        tmpQNode
->prevQNode->nextQNode = tmpQNode->nextQNode;

    
if (tmpQNode->nextQNode == NULL) // is the tail
        _queueTail = tmpQNode->prevQNode;
    
else
        tmpQNode
->nextQNode->prevQNode = tmpQNode->prevQNode;

    tmpNode 
= tmpQNode->node;
    delete tmpQNode;
    hcur 
= NULL;
    
return tmpNode;
}

posted on 2004-11-12 13:49  玄驹子  阅读(3548)  评论(4)    收藏  举报