23.2 MFC utility classes

Unlike most classes, the handy utility classes, CRect , CPoint , and CSize , have public data members . That is because these classes are actually derived as child classes of the old Windows structures RECT , POINT , and SIZE , respectively. This is permissible in C++ because a structure can be thought of as a class all of whose members are public. The names of the data fields of these three classes are shown in Table 23.1.

Table 23.1. The fields of some simple MFC utility classes.

Class name

Public data fields


left , top , right , bottom


x , y


cx , cy

These utility classes have some nice accessors and overloaded operators. For instance, we can get the CSize csmax of a CRect crbox by writing

 CSize csmax = crbox.Size(); 

CString is a particularly useful MFC class. The CString takes the place of the char[] that you might otherwise use to store a string. A CString is a self-sizing array, so you don't need to preallocate memory space before you put characters into it. Using the CString's constructor we might instead write something like

 CString cstrhello("Pop!"); 

Another thing worth noting is that the operator== is overloaded for CString so that stringa == stringb means that they have the same characters.

The MFC code for these utility classes is formulated in a sufficiently polymorphic way that you can usually pass these objects without an explicit & reference cast even if the calling function expects a pointer. In other words, MFC functions that are prototyped for a CRect * argument will also accept a CRect argument, passed by reference as a CRect& . A function that wants a data type with the kludgy old name LPCTSTR (for 'long pointer to a constant string') will accept a simple CString argument.

Software Engineering and Computer Games
Software Engineering and Computer Games
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net