Registrations are now open. Join us today!
There is still a lot of work to do on the wiki yet! More information about editing can be found here.
Already have an account?

Microsoft KB Archive/102327

From BetaArchive Wiki
Knowledge Base


Article ID: 102327

Article Last Modified on 11/21/2006



APPLIES TO

  • Microsoft Foundation Class Library 4.2, when used with:
    • Microsoft C/C++ Professional Development System 7.0
    • Microsoft Visual C++ 6.1
    • Microsoft Visual C++ 6.0 Professional Edition
    • Microsoft Visual C++ 6.0 Enterprise Edition
    • Microsoft Visual C++ 5.0 Learning Edition



This article was previously published under Q102327

7.00 | 1.00 1.50 1.51 1.52 | 1.00 2.00 2.10 4.00 MS-DOS | WINDOWS | WINDOWS NT kbprg

SUMMARY

When you write an application with the CObList class provided by the Microsoft Foundation Class (MFC) libraries, you may note that many of the member functions have both const and non-const forms. For example, the CObList class has two GetNext() member functions, as follows:

   CObject * & GetNext(POSITION & rPosition);
   CObject *   GetNext(POSITION & rPosition) const;
                



The first form of the GetNext() member function is for non-const CObList objects and implements the left-side of the assignment operator. The second form of the GetNext() member function is used when the CObList object is const (generally, this means that you are using a pointer to a const CObList to prevent any modification to the contents of the CObList).

Note that the first member function returns a reference to a CObject pointer. The item returned is the actual CObject pointer in the list, not its value. Through this reference, you can modify the CObject pointer and use the return value of the member function to modify the list. The second member function does not provide access to modify the CObList.

MORE INFORMATION

The code sample below demonstrates using the GetNext() member function that returns a reference to a CObject pointer. Note the comment in the code. If you remove the comment from the indicated line, an error results.

Sample Code



// 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>
                



class CMyObject: public CObject { public:

   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