次の方法で共有


CObject Class: Specifying Levels of Functionality

OverviewHow Do IFAQ

This article describes how to add the following levels of functionality to your -derived class:

  • Run-time class information

  • Dynamic creation support

  • Serialization support

For a general description of CObject functionality, see the article CObject Class: Deriving a Class from CObject.

To add run-time class information

CObject supports run-time class information through the IsKindOf function, which allows you to determine if an object belongs to or is derived from a specified class. For more detailed information, see the articles Files in MFC and Serialization (Object Persistence). This capability is not supported directly by the C++ language. The IsKindOf function permits you to do a type-safe cast down to a derived class.

Use the following steps to access run-time class information:

  1. Derive your class from CObject, as described in the CObject Class: Deriving a Class from CObject article.

  2. Use the DECLARE_DYNAMIC macro in your class declaration, as shown here:

    class CPerson : public CObject
    {
        DECLARE_DYNAMIC( CPerson )
    
        // rest of class declaration follows...
    };
    
  3. Use the IMPLEMENT_DYNAMIC macro in the implementation file (.CPP) of your class. This macro takes as arguments the name of the class and its base class, as follows:

    IMPLEMENT_DYNAMIC( CPerson, CObject )
    

Note   Always put IMPLEMENT_DYNAMIC in the implementation file (.CPP) for your class. The IMPLEMENT_DYNAMIC macro should be evaluated only once during a compilation and therefore should not be used in an interface file (.H) that could potentially be included in more than one file.

To add dynamic creation support

CObject also supports dynamic creation, which is the process of creating an object of a specific class at run time. The object is created by the CreateObject member function of CRuntimeClass. Your document, view, and frame class should support dynamic creation because the framework (through the CDocTemplate class) needs to create them dynamically. Dynamic creation is not supported directly by the C++ language. To add dynamic creation, you must do the following:

  1. Derive your class from CObject.

  2. Use the DECLARE_DYNCREATE macro in the class declaration.

  3. Define a constructor with no arguments (a default constructor).

  4. Use the IMPLEMENT_DYNCREATE macro in the class implementation file.

To add serialization support

“Serialization” is the process of writing or reading the contents of an object to and from a file. The Microsoft Foundation Class Library uses an object of the CArchive class as an intermediary between the object to be serialized and the storage medium. The CArchive object uses overloaded insertion (<<) and extraction (>>) operators to perform writing and reading operations.

The following steps are required to support serialization in your classes:

  1. Derive your class from CObject.

  2. Override the Serialize member function.

    ****Note   ****If you call Serialize directly, that is, you do not want to serialize the object through a polymorphic pointer, omit steps 3 through 5.

  3. Use the DECLARE_SERIAL macro in the class declaration.

  4. Define a constructor with no arguments (a default constructor).

  5. Use the IMPLEMENT_SERIAL macro in the class implementation file.

****Note   ****A “polymorphic pointer” points to an object of a class (call it A) or to an object of any class derived from A (say, B). To serialize through a polymorphic pointer, the framework must determine the run-time class of the object it is serializing (B), since it might be an object of any class derived from some base class (A).

For more details on how to enable serialization when you derive your class from CObject, see the articles Files in MFC and Serialization (Object Persistence).