Microsoft KB Archive/102327

= INFO: Const Versus Non-Const CObList Objects =

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

 Microsoft Visual C++ 5.0 Learning Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q102327

<div class="notice_section">

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

<div class="summary_section">

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:

<pre class="fixed_text">  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.

<div class="moreinformation_section">

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


 * 1) define _DOS
 * 2) include <iostream.h>
 * 3) include <afx.h>
 * 4) include <afxcoll.h>

class CMyObject: public CObject { public: <pre class="fixed_text">  CMyObject(int i):set(i) { } int set; } MyObject(2); // Create a CObject to place in the CObList. // assign set = 2

void main(void) { <pre class="fixed_text">  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

<pre class="fixed_text">  for (POSITION p = pMyList->GetHeadPosition; p != NULL; )

cout << ((CMyObject*)pMyList->GetNext(p))->set;

<pre class="fixed_text">  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;

<pre class="fixed_text">  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

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.