Thursday, August 02, 2007

The real use of C++ class concept in MFC :

The real use of C++ class in MFC :

First windows developed the win32 programming model.

But it is so complicated.. It is being simplified by MFC. MFC is nothing but an object oriented framework around WIN32 API.

 

 

 

Take an Example :

 

GetWindowRect( LPRECT lpRect);

these two functions are APIs in windows.

LPRECT is a pointer to the RECT structure .

typedef RECT* LPRECT;

 

In MFC, there is a class CRect for handling rectangles.

in MFC code they may as follows :

CRect rc;

GetWindowRect(rc);

it will works well without any problem. Actually the GetWindowRect() API supports only LPRECT as argument...

Is it possible to develope our own Rectangle class in our application ?... yes...

 

How ?

it is the speciality of Operator conversion.

 

class CRect : public RECT

{

public :

operator LPRECT()

{

return this;

}

};

while executing the following line,

CRect rc;

GetWindowRect(rc);

rc Object is converted to the LPRECT, so the following code in a CRect class will be executed.

operator LPRECT()

{

return this;

}

So the pointer of the RECT derived class is passed as an argument to the GetWindowRect() fn.

 

if we are writing our own rectangle class

class CMyRect : public RECT

{

public:

operator LPRECT()

{

return this;

}

};

what is the purpose of providing wrapper class around RECT like structures in Windows ?

In order to simplify the operation, we are going for the wrapper class.

For Example,

RECT structure has following members

struct RECT

{

int left;

int top;

int bottom;

int right;

};

if we want to idenify the Width of the rectangle every time the user has to calculate it ...

Instead of it we develope the wrapper class method as follows that will be more easy...

LONG CRect :: Width()

{

return right - left;

}

Actually RECT structure is simple ... Windows has so much complicated structures to be passed as an argument to the

Windows API function. At that time, it will be more helpful...

No comments: