gpt4 book ai didi

c++ - CAtlList::RemoveAt 是否会使现有位置无效?

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:54 27 4
gpt4 key购买 nike

我正在看这个,其中 m_Rows 是一个 CAtlList:

void CData::RemoveAll()
{
size_t cItems = m_Rows.GetCount();
POSITION Pos = m_Rows.GetHeadPosition();

while(Pos != 0)
{
CItem* pItem = m_Rows.GetAt(Pos);

if (pItem != 0)
delete pItem;

POSITION RemoveablePos = Pos;
pItem = m_Rows.GetNext(Pos);

m_Rows.RemoveAt(RemoveablePos);
}
}

我想知道 RemoveAt 调用是否有可能使 Pos 无效?

最佳答案

根据documentation , CAtlList 的行为类似于双链表,因此删除一个列表项应该 不会使指向其他项的指针无效。 POSITION 类型直接引用列表项的内存位置:

Most of the CAtlList methods make use of a position value. This value is used by the methods to reference the actual memory location where the elements are stored, and should not be calculated or predicted directly.

在atlcoll.h中好像不是这样的:

template< typename E, class ETraits >
void CAtlList< E, ETraits >::RemoveAt( POSITION pos )
{
ATLASSERT_VALID(this);
ATLENSURE( pos != NULL );

CNode* pOldNode = (CNode*)pos;

// remove pOldNode from list
if( pOldNode == m_pHead )
{
m_pHead = pOldNode->m_pNext;
}
else
{
ATLASSERT( AtlIsValidAddress( pOldNode->m_pPrev, sizeof(CNode) ));
pOldNode->m_pPrev->m_pNext = pOldNode->m_pNext;
}
if( pOldNode == m_pTail )
{
m_pTail = pOldNode->m_pPrev;
}
else
{
ATLASSERT( AtlIsValidAddress( pOldNode->m_pNext, sizeof(CNode) ));
pOldNode->m_pNext->m_pPrev = pOldNode->m_pPrev;
}
FreeNode( pOldNode );
}

关于c++ - CAtlList::RemoveAt 是否会使现有位置无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1847717/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com