Article ID: 102327
Article Last Modified on 11/21/2006
CObject * & GetNext(POSITION & rPosition); CObject * GetNext(POSITION & rPosition) const;
// Sample program to create a CObList, fill it, display the "set" // member of each CObject pointed to by the CObList, reassign the // elements of the CObList, and display the "set" member of each // CObject pointed to by the CObList a second time.
/** Compiler options needed: Visual C++ 4.0 - /MT[d] or /MD[d] * Others - None
*/
#define _DOS #include <iostream.h> #include <afx.h> #include <afxcoll.h>
CMyObject(int i):set(i) { }
int set;
} MyObject(2); // Create a CObject to place in the CObList.
// assign set = 2
void main(void){
CObList * pMyList = new CObList; const CObList * pMyListAlso = pMyList;
// load list
for (int nCount = 0; nCount < 10; nCount++)
pMyList->AddHead(new CMyObject(1)); // Fill up list with
// CMyObject objects that have set equal to 1
for (POSITION p = pMyList->GetHeadPosition(); p != NULL; )
cout << ((CMyObject*)pMyList->GetNext(p))->set;
for (p = pMyList->GetHeadPosition(); p != NULL; )
(pMyList->GetNext(p)) = &MyObject;
// If you remove the comments from the following lines, the compiler // generates the following error: // // error C2106: '=' : left operand must be lvalue // // The error occurs because pMyListAlso is a pointer to a const // CObList object and the GetNext() member function that returns a // CObject pointer is called. // // for (p = pMyList->GetHeadPosition(); p != NULL; ) // (pMyListAlso->GetNext(p)) = &MyObject;
for (p = pMyList->GetHeadPosition(); p != NULL; )
cout << ((CMyObject*)pMyList->GetNext(p))->set;
Additional query words: 7.00 1.00 1.50 2.00 2.10 2.50 2.51 2.52 3.00 kbinf 4.00
Keywords: kbcollectionclass kbinfo KB102327