if (pOb =
本人在阅读CArchive源代码时遇到问题,请求高手解答....
首先,先看我给出的代码CArchive::WriteObject 和 CMapPtrToPtr::operator[]等..。
void CArchive::WriteObject(const CObject* pOb)
{
......//
if (pOb == NULL)
{
// save out null tag to represent NULL pointer
*this << wNullTag;
}
else if ((nObIndex = (DWORD)(*m_pStoreMap)[(void*)pOb]) != 0)
// assumes initialized to 0 map
{
// save out index of already stored object
if (nObIndex < wBigObjectTag)
*this << (WORD)nObIndex;
else
{
*this << wBigObjectTag;
*this << nObIndex;
}
}
else
{
// write class of object first
CRuntimeClass* pClassRef = pOb->GetRuntimeClass();
WriteClass(pClassRef);
// enter in stored object table, checking for overflow
CheckCount();
(*m_pStoreMap)[(void*)pOb] = (void*)m_nMapCount++;
// cause the object to serialize itself
((CObject*)pOb)->Serialize(*this);
}
}
void*& CMapPtrToPtr::operator[](void* key)
{
ASSERT_VALID(this);
UINT nHash;
CAssoc* pAssoc;
if ((pAssoc = GetAssocAt(key, nHash)) == NULL)
{
if (m_pHashTable == NULL)
InitHashTable(m_nHashTableSize);
// it doesn't exist, add a new Association
pAssoc = NewAssoc();
pAssoc->key = key;
// 'pAssoc->value' is a constructed object, nothing more
// put into hash table
pAssoc->pNext = m_pHashTable[nHash];
m_pHashTable[nHash] = pAssoc;
}
return pAssoc->value; // return new reference
}
CMapPtrToPtr::CAssoc* CMapPtrToPtr::NewAssoc()
{
......//省略。
pAssoc->key = 0;
pAssoc->value = 0;
return pAssoc;
}
问题1.
在CArchive::WriteObject中,nObIndex = (DWORD)(*m_pStoreMap)[(void*)pOb]) != 0返回的是一个索引,而(*m_pStoreMap)[(void*)pOb] = (void*)m_nMapCount++; 赋的值是Map的长度(大小),m_pStoreMap是CMapPtrToPtr类,并该类型且重载了“[]”运算符,而从CMapPtrToPtr::operator[]返回的是m_pHashTable的某一项(CAssoc类型)的value值(自己看一下源代码,因为字数有限,不多写)。那么按照我的理解,(*m_pStoreMap)[(void*)pOb] = (void*)m_nMapCount++; 是把m_nMapCount赋值给m_pHashTable的某一项(CAssoc类型)的value,就是说CAssoc.value是存储m_pHashTable的索引的,这是我的理解。
想问的是,既然上面的代码中nObIndex = (DWORD)(*m_pStoreMap)[(void*)pOb]) != 0 是返回一个索引,那么为什么还判断 if (nObIndex < wBigObjectTag) 呢,不是m_nMapCount的长度一定不会大于wBigObjectTag(0x7FFF)吗??
问题2.
(*m_pStoreMap)[(void*)pOb] = (void*)m_nMapCount++; 是不是想把m_pStoreMap长度赋值给m_pStoreMap某一项的CAssoc.value啊,为什么呢?
问题3.
wBigObjectTag、wNullTag、wNewClassTag、wClassTag等,这些是什么东西,具体有什么作用,我想清楚点...??
问题4.
怎么确定什么时候用wBigObjectTag,什么时候用wNewClassTag等...他们靠什么作为依据呢??还有可以详细解析一下CArchive::WriteObject 和CArchive::WriteClass的源代码??
先谢谢高手......