自已定義一個.h文件,然後,在裡面加入類的定義
class CPartA : public CObject
{
public:
CPartA() {}
virtual ~CPartA() {}
int m_a;
int m_b;
CString m_c;
int m_d;
};
typedef CTypedPtrList<CObList, CPartA*> CPartAList;//重點在這兒
class CInfoA
{
public:
CInfoA() {}
virtual ~CInfoA();
BOOL AddData(int a, int b, CString c, int d); //用於加入數據的
CPartA m_objects;
};
在.cpp裡
CInfoA的析構
CInfoA::~CInfoA()
{
POSITION pos = m_objects.GetHeadPosition();
while (pos != NULL)
delete m_objects.GetNext(pos);
}
//加入到鏈表
CInfoA::AddData(int a, int b, CString c, int d)
{
CPartA* pPart = new CPartA();
pPart->m_a = a;
pPart->m_b = b;
pPart->m_c = c;
pPart->m_d = d;
m_objects.AddTail(pPart);
return TRUE;
}
自已來引申,一定對你有用處的,當你在程序中要用到這個鏈表時
你只要在.h裡定義
CInfoA m_InfoA
在.cpp裡
m_Info.AddPart(1, 2, "ddd", 3);
就可以加入了
3.遍歷這個鏈表
你在程序中的函數加入下面的語句即可
POSITION pos;
pos = m_InfoA.m_objects.GetHeadPosition();
while (pos != NULL)
{
CPartA* pPart = m_InfoA.m_objects.GetNext(pos);
if (pPart->m_a != 0)
........
}