MFC CRuntimeClass 学习

新建一个控制台工程,按以下步骤;

    定义对象Person,继承CObject,在头文件中添加宏:DECLARE_DYNCREATE(Person)

    在源文件中添加宏:IMPLEMENT_DYNCREATE(Person, CObject)

    main.cpp中,

        使用宏:CRuntimeClass* pRuntimeClass  = RUNTIME_CLASS(Person);运行时类信息

        使用运行时类信息创建对象:Person* pPerson = (Person*)pRuntimeClass->CreateObject();

代码如下;

Person.h,

#pragma once

#define _AFXDLL
#include 
#include 

class Person : public CObject
{
protected:
	Person() noexcept; //无异常抛出
	DECLARE_DYNCREATE(Person)

public:
	long m_id;
	CString m_name;
	int m_age;
};

Person.cpp,

#include "Person.h"

IMPLEMENT_DYNCREATE(Person, CObject)
Person::Person() noexcept
{
	m_id = 1;
	m_age = 102;
	m_name = _T("John");
}

main.cpp;

你可能感兴趣的:(VC++,mfc,c++,CRuntimeClass,运行时类信息)