Editéieren

Deelen iwwer


Dynamic Object Creation

Note

The Microsoft Foundation Classes (MFC) library continues to be supported. However, we're no longer adding features or updating the documentation.

This article explains how to create an object dynamically at run time. The procedure uses run-time class information, as discussed in the article Accessing Run-Time Class Information.

Dynamically create an object given its run-time class

  1. Use the following code to dynamically create an object using the CreateObject function of the CRuntimeClass. On failure, CreateObject returns NULL instead of raising an exception:

    CRuntimeClass* pRuntimeClass = RUNTIME_CLASS(CMyClass);
    CObject* pObject = pRuntimeClass->CreateObject();
    ASSERT(pObject->IsKindOf(RUNTIME_CLASS(CMyClass)));
    

See also

Destroying Window Objects
Using CObject