Partager via


CMapStringToOb::operator

A convenient substitute for the SetAt member function.

CObject*& operator [ ]( 
   LPCTSTR key  
);

Return Value

A reference to a pointer to a CObject object; or NULL if the map is empty or key is out of range.

Remarks

Thus it can be used only on the left side of an assignment statement (an l-value). If there is no map element with the specified key, then a new element is created.

There is no "right side" (r-value) equivalent to this operator because there is a possibility that a key may not be found in the map. Use the Lookup member function for element retrieval.

The following table shows other member functions that are similar to CMapStringToOb::operator [].

Class

Member Function

CMapPtrToPtr

void*& operator[]( void* key );

CMapPtrToWord

WORD& operator[]( void* key );

CMapStringToPtr

void*& operator[]( LPCTSTR key );

CMapStringToString

CString& operator[]( LPCTSTR key );

CMapWordToOb

CObject*& operator[]( WORD key );

CMapWordToPtr

void*& operator[]( WORD key );

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

CMapStringToOb map;

map[_T("Bart")] = new CAge(13);
map[_T("Lisa")] = new CAge(11);
#ifdef _DEBUG
   afxDump.SetDepth(1);
   afxDump << _T("Operator [] example: ") << &map << _T("\n");
#endif         

The results from this program are as follows:

Operator [] example: A CMapStringToOb with 2 elements

[Lisa] = a CAge at $4A02 11

[Bart] = a CAge at $497E 13

Requirements

Header: afxcoll.h

See Also

Reference

CMapStringToOb Class

Hierarchy Chart

CMapStringToOb::SetAt

CMapStringToOb::Lookup