Skip to main content

What is MFC& and CObject

MFC stands for microsoft foundation class ,Its a microsoft class library ,its a wrapper of the API ,its use for creating Windows application( GUI application) in Windows .basiccally in MFC there is two class CFrameWnd and CWinApp
CframeWnd is provide the funcitonalities related to  GUI and CWinapp for application level functionalities ..
Both these classes derived from CCmdTarget .CCmdTarget is created with the capability to handle windows messages,which is referred as Message Maps.CCmdTarget is derived from CObject
CObject:-
              its main root class for major MFC class.it provides these major features in your own class Object  like as Serialization support,run time class information and object diagnostic output. CObject class has the ability to determine the exact class of an object at run time.
Some classes are not derived form the CObject like CString ,CTime,CTimespan 


CObject Serialization support:-
when a class is derived from COject there is two main MACRO use for the serialization object.
DECLARE_SERIAL and IMPLEMENT_SERIAL
Reads or writes this object from or to an archive.
function signature is
virtual void Serialize(CArchive & ar)
{


}
here is ar is serialize to or from.
You must override Serialize for each class that you intend to serialize. The overridden Serialize must first call theSerialize function of its base class.


For ex:-


void CMyClass:: Serialize(CArchive& ar )
{
CObject::Serialize( ar );
if(ar.IsStoring())
{
arr<<myClass;
}
else
arr>>myClass;
}




COject is associated with the  CRuntimeClass use can use to obtain  the inforamation about  an object  or its base class at Run time .
CRuntimeCLass is a struct so it is does not contain any base class.

struct CRuntimeClass
The ability to determine the class of an object at run time is useful when extra type checking of function arguments is needed, or when you must write special-purpose code based on the class of an object. Run-time class information is not supported directly by the C++ language.
CRuntimeClass* pClass = RUNTIME_CLASS( CObject );
the IsKindOf member function for objects of that class, using the RUNTIME_CLASS macro to generate theCRuntimeClass argument, as shown here:
class CPerson : public CObject
{
    DECLARE_DYNAMIC( CPerson )
public:
    CPerson(){};

    // other declaration 
};

// in .CPP file
IMPLEMENT_DYNAMIC( CPerson, CObject )

void SomeFunction(void)
{
   CObject* pMyObject = new CPerson;

   if(pMyObject->IsKindOf( RUNTIME_CLASS( CPerson ) ) )
   {
      //if IsKindOf is true, then cast is all right
      CPerson* pmyPerson = (CPerson*) pMyObject ;
      ...
      delete pmyPerson;
   }
...
   delete [MyObject];
}

Comments

Popular posts from this blog

Microservice Architecture

Agile development & Deployment is difficult in case of ____ Monolithic Software built as microservices can, by definition, be broken down into multiple component services ? True Separating components with conflicting resource requirements falls under the bucket of _ Microservices Complexity of developing, testing & deploying distributed system, Handling partial failures account to disadvantages of Microservices Benefits of Microservices include - All the options Decomposition of Microservices based on 2 categories namely Business capability , Subdomain Simple to Develop, Test, Deploy, Scale represents ____ Monolithic Is Microservice is considered as subset of SOA ? True The 3Cs of Microservices includes all these except Control Microservice Architecture adapts following concepts All the options The client expects a timely response from the service and might even block while it waits represents__client service interaction styles Synch...

What is Document/View Architecture in MFC?

By default MFC application wizard creats with an skeleton with a document class and another is view class.MFC seperates data management into these two class.The document class store the data and print the data and view display the data and manages the user interaction with it. Notepad is simple example for SDI application. or ALL MFC application which have user interface follow this architecture. Any MFC application creates with two basic skeleton one is document class and another is view class.document manages the data for multiple view,and view class displays the data. MFC document/view architecture makes it easy to support multiple view,multiple document types, splitter windows and other valuable user interface features. . For exm:- in SDI application we have following major class:- 1. CMainFrameApp:- Frame class derived  from the CFrameWnd 2. CMySDIView :- View class derived  from the CView class 3. CMainDoc :- Document class derived  from the CDoc...