2009. 8. 27. 16:10
Programming/MFC
DYNAMIC
GetRuntimeObject사용 가능- DECLARE_DYNAMIC을 선언해야 사용 가능
/* CAge.h */ class CAge : public CObject { int num; public: DECLARE_DYNAMIC(CAge) }; //============== /* CAge.cpp */ #include "stdafx.h" #include "CAge.h" IMPLEMENT_DYNAMIC(CAge, CObject)
사용 예 1
// example for CObject::GetRuntimeClass // Example for RUNTIME_CLASS CRuntimeClass* prt = RUNTIME_CLASS( CAge ); ASSERT( lstrcmp( prt->m_lpszClassName, "CAge" ) == 0 );
사용 예 2
CAge a(21); CRuntimeClass* prt = a.GetRuntimeClass(); ASSERT( strcmp( prt->m_lpszClassName, "CAge" ) == 0 );
사용 예 3
// example for CObject::IsKindOf CAge a(21); // Must use IMPLEMENT_DYNAMIC, IMPLEMENT _DYNCREATE, or // IMPLEMENT_SERIAL ASSERT( a.IsKindOf( RUNTIME_CLASS( CAge ) ) ); ASSERT( a.IsKindOf( RUNTIME_CLASS( CObject ) ) );
DYNCREATE
- 동적으로 객체를 생성할 필요가 있을 때 (
CreateObject)
CRuntimeClass* pRuntimeClass = RUNTIME_CLASS( CMyClass ); CObject* pObject = pRuntimeClass->CreateObject(); ASSERT( pObject->IsKindOf( RUNTIME_CLASS( CMyClass ) ) );
선언
/* CAge.h */ class CAge : public CObject { int num; public: DECLARE_DYNCREATE(CAge) }; //============== /* CAge.cpp */ #include "stdafx.h" #include "CAge.h" IMPLEMENT_DYNCREATE(CAge, CObject)
사용 예
// This example creates an object from the run-time class. It only // creates objects derived from CWnd. // We only want to create an object derived from CWnd. if (!pClass->IsDerivedFrom(RUNTIME_CLASS(CWnd))) { TRACE(_T("Error; Object %s is not derived from CWnd\n")); return; } // Get a pointer to the base class CRuntimeClass. #ifdef _AFXDLL CRuntimeClass* pBaseClass = pClass->m_pfnGetBaseClass(); #else CRuntimeClass* pBaseClass = pClass->m_pBaseClass; #endif ASSERT(pBaseClass != NULL); TRACE(_T("Creating object %s derived from %s, with object size %d and schema %d\n"), pClass->m_lpszClassName, pBaseClass->m_lpszClassName, pClass->m_nObjectSize, pClass->m_wSchema); // Create the object. CObject* pObject = pClass->CreateObject();
SERIAL
DECLARE_SERIAL
class CMyClass: public CObject { public: CMyClass( ); void Serialize( CArchive& archive ); DECLARE_SERIAL( CmyClass ) };
IMPLEMENT_SERIAL
/* MyClass.cpp */ #include "stdafx.h" #include "MyClass.h" IMPLEMENT_SERIAL( CMyClass, CObject, VERSIONABLE_SCHEMA | 2 ) ...
Version
- 선언
class CPerson : public CObject { DECLARE_SERIAL( CPerson ) // rest of declaration follows... };
- Version 생성
IMPLEMENT_SERIAL( CPerson, CObject, VERSIONABLE_SCHEMA|1 ) void CPerson::Serialize( CArchive& ar ) { CObject::Serialize( ar ); // Always call base class Serialize. // Serialize dynamic members and other raw data if ( ar.IsStoring() ) { int nVersion = ar.GetObjectSchema(); switch(nVersion) { case 0 : .... case 1 : .... } else { } }