Programming Windows with MFC - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Programming Windows with MFC

Description:

Convenient implementations of arrays, linked lists, maps, and other containers ... Populate the array, growing it as needed. for (int i=0; i 10; i ) ... – PowerPoint PPT presentation

Number of Views:162
Avg rating:3.0/5.0
Slides: 25
Provided by: Jos421
Category:

less

Transcript and Presenter's Notes

Title: Programming Windows with MFC


1
Programming Windows with MFC
2
Chapter 5
  • The MFC Collection Classes

3
The Standard Template Library
  • Convenient implementations of arrays, linked
    lists, maps, and other containers
  • A container in the language of STL
  • An object that stores collections of data

4
The MFC Collection Classes
  • MFC collection classes
  • own implementations of arrays, linked lists, and
    maps in a family of classes
  • Never have to write a linked list

5
Arrays
6
Arrays
  • Access violation

int array10 for (int i0 ilt10 i)
arrayi i 1
  • What happened when i 10?

7
Arrays
  • Get and Set functions

class CArray int Get (int nIndex)
assert (nIndex gt 0 nIndex lt m_nSize)
return m_pDatanIndex void Set (int
nIndex, int nVal) assert (nIndex gt 0
nIndex lt m_nSize) m_pDatanIndex nVal

CArray array (10) for (int i0 ilt10 i)
array.Set (i, i 1) // Asserts when i 10.
8
The MFC Array Classes
  • CArray class
  • A template class
  • Create type-safe arrays
  • Defined in the header file Afxtempl.h
  • Nontemplatized array classes
  • Designed to hold a particular type of data
  • defined in Afxcoll.h

9
Type-Specific MFC Array Classes
10
Usages
  • SetSize
  • InsertAt
  • RemoveAt
  • RemoveAll
  • CUIntArray array
  • array.SetSize (10)
  • for (int i0 ilt10 i)
  • arrayi i 1

11
Dynamic Array Sizing
  • SetSize
  • SetAtGrow
  • Add
  • InsertAt
  • Append
  • Copy

12
CMemoryExceptions
  • try
  • CUIntArray array
  • // Might throw a CMemoryException.
  • array.SetSize (1000)
  • catch (CMemoryException e)
  • AfxMessageBox (_T ("Error
  • Insufficient memory"))
  • // Delete the exception object.
  • e-gtDelete ()

13
Creating Type-Safe Array Classes with CArray
  • Your own MFCs CArray class

CArrayltCPoint, CPointgt array // Populate the
array, growing it as needed. for (int i0 ilt10
i) array.SetAtGrow (i, CPoint (i10, 0))
// Enumerate the items in the array. int
nCount array.GetSize () for (i0 iltnCount
i) CPoint point arrayi TRACE (_T
("xd, yd\n"), point.x, point.y)
14
Lists
15
Linked list
  • Support fast item insertion and removal
  • A collection of items that contain pointers to
    other items

item pItem GetHead () while (pItem ! NULL)
pItem pItem-gtpNextItem item pItem
GetTail () while (pItem ! NULL) pItem
pItem-gtpPrevItem
16
Type-Specific MFC List Classes
17
Lists
  • AddHead
  • RemoveHead/RemoveTail/RemoveAll
  • GetNex/GetPrev
  • GetHeadPosition/GetTailPosition
  • Find

18
Creating Type-Safe List Classes with CList
  • CListltCPoint, CPointgt list
  • // Populate the list.
  • for (int i0 ilt10 i)
  • list.AddTail (CPoint (i10, 0))
  • // Enumerate the items in the list.
  • POSITION pos list.GetHeadPosition ()
  • while (pos ! NULL)
  • CPoint point list.GetNext (pos)
  • TRACE (_T ("xd, yd\n"), point.x, point.y)

19
Maps
  • Look-up table

20
Type-Specific MFC Map Classes
21
CMapStringToString
  • CMapStringToString map
  • map_T("Sunday") _T("Dimanche")
  • map_T("Monday") _T("Lundi")
  • map_T("Tuesday") _T("Mardi")
  • map_T("Wednesday") _T("Mercredi")
  • map_T("Thursday") _T("Jeudi")
  • map_T("Friday") _T("Vendredi")
  • map_T("Saturday") _T("Samedi")
  • CString string
  • if (map.Lookup (_T ("Thursday"), string))
  • TRACE (_T ("Thursday in English s in
    French\n"), string)

22
The Typed Pointer Classes
  • A set of three template classes designed to
    handle collections of pointers in a type-safe
    manner

23
Collection Classes for Pointers
24
CObList
  • CTypedPtrListltCObList, CLinegt list
  • // Populate the list.
  • for (int i0 ilt10 i)
  • int x i 10
  • CLine pLine new CLine (x, 0, x, 100)
  • list.AddTail (pLine)
  • // Enumerate the items in the list.
  • POSITION pos list.GetHeadPosition ()
  • while (pos ! NULL)
  • CLine pLine list.GetNext (pos) // No
    casting!
Write a Comment
User Comments (0)
About PowerShow.com