Advanced Preprocessor Statements

Chapter 22 - The Microsoft Foundation Class Library: Fundamentals

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

It All Begins with CObject
Libraries such as the MFC library often start with a few parent classes. Additional classes are then derived from the parent classes. CObject is one parent class used extensively in developing Windows applications. The MFC library header files located in the MFC/INCLUDE subdirectory provide a wealth of information on defined classes.
Let’s take a brief look at an edited version of Cobject that is defined in the AFX.H header file:
/////////////////////////////////////////////////////////////
// class CObject is the root of all compliant objects

class CObject
{
public:

// Object model (types, destruction, allocation)
 virtual CRuntimeClass* GetRuntimeClass( ) const;
 virtual ~CObject( );  // virtual destructors are necessary

 // Diagnostic allocations
 void* PASCAL operator new(size_t nSize);
 void* PASCAL operator new(size_t, void* p);
 void PASCAL operator delete(void* p);
#if defined(_DEBUG) && !defined(_AFX_NO_DEBUG_CRT)
 // for file name/line number tracking using DEBUG_NEW
 void* PASCAL operator new(size_t nSize,
                           LPCSTR lpszFileName,
                           int nLine);
#endif

// Disable the copy constructor and assignment by default
// so you will get compiler errors instead of unexpected
// behavior if you pass objects by value or assign objects.

protected:
 CObject( );
private:
 CObject(const CObject& objectSrc);     //no implementation
 void operator=(const CObject& objectSrc);

// Attributes
public:
 BOOL IsSerializable( ) const;
 BOOL IsKindOf(const CRuntimeClass* pClass) const;
// Overridables
 virtual void Serialize(CArchive& ar);

 // Diagnostic Support
 virtual void AssertValid( ) const;
 virtual void Dump(CDumpContext& dc) const;

// Implementation
public:
 static const AFX_DATA CRuntimeClass classCObject;
#ifdef _AFXDLL
 static CRuntimeClass* PASCAL _GetBaseClass( );
#endif
};
This code has been edited slightly for clarity, but is essentially the same code that you will find in the header file.
Upon inspection of the CObject listing, notice the components that make up this class definition. First, CObject is divided into public, protected, and private parts. CObject also provides normal and dynamic type checking and serialization. Recall that dynamic type checking allows the type of object to be determined at run time. The state of the object can be saved to a storage medium, such as a disk, through a concept called persistence. Object persistence allows object member functions to also be persistent, permitting retrieval of object data.
Child classes are derived from parent classes. CGdiObject is an example of a class derived from CObject. Here is the CGdiObject definition as found in AFXWIN.H. Again, this listing has been edited for clarity.
/////////////////////////////////////////////////////////////
// CGdiObject abstract class for CDC SelectObject

class CGdiObject : public CObject
{
 DECLARE_DYNCREATE(CGdiObject)
public:

// Attributes
 HGDIOBJ m_hObject;  // must be first data member
 operator HGDIOBJ( ) const;
 HGDIOBJ GetSafeHandle( ) const;
 static CGdiObject* PASCAL FromHandle(HGDIOBJ hObject);
 static void PASCAL DeleteTempMap( );
 BOOL Attach(HGDIOBJ hObject);
 HGDIOBJ Detach( );

// Constructors
 CGdiObject( ); // must create a derived class object
 BOOL DeleteObject( );

// Operations
 int GetObject(int nCount, LPVOID lpObject) const;
 UINT GetObjectType( ) const;
 BOOL CreateStockObject(int nIndex);
 BOOL UnrealizeObject( );
 BOOL operator==(const CGdiObject& obj) const;
 BOOL operator!=(const CGdiObject& obj) const;
// Implementation
public:
 virtual ~CGdiObject( );
#ifdef _DEBUG
 virtual void Dump(CDumpContext& dc) const;
 virtual void AssertValid( ) const;
#endif
};
CGdiObject and its member functions (methods) allow drawing items such as stock and custom pens, brushes, and fonts to be created and used in a Windows application. Classes such as CPen are further derived from the CGdiObject class.
Microsoft has provided complete source code for the MFC library in order to allow the utmost in programming flexibility and customization. However, for the beginner, it is not even necessary to know how the various classes are defined in order to use them efficiently.
For example, in traditional procedure-oriented Windows applications, the DeleteObject( ) function is called with the following syntax:
DeleteObject(hBRUSH);  /*hBRUSH is the brush handle*/
In object-oriented applications, using the MFC library, the same results can be achieved by accessing the member function with the following syntax:
newbrush.DeleteObject( ); //newbrush is current brush
As you can see, switching between procedure-oriented Windows function calls and class library objects can be intuitive. Microsoft has used this approach in developing all Windows classes, making the transition from traditional function calls to MFC library objects very easy.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

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