CMapStringToOb::operator [ ]
A convenient substitute for the SetAt member function.
CObject*& operator [ ](
LPCTSTR key
);
Возвращаемое значение
A reference to a pointer to a CObject object; or NULL if the map is empty or key is out of range.
Заметки
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 |
---|---|
void*& operator[]( void* key ); |
|
WORD& operator[]( void* key ); |
|
void*& operator[]( LPCTSTR key ); |
|
CString& operator[]( LPCTSTR key ); |
|
CObject*& operator[]( WORD key ); |
|
void*& operator[]( WORD key ); |
Пример
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
Требования
Header: afxcoll.h