Serializing an Abstract Base ClassLast reviewed: October 10, 1997Article ID: Q103983 |
1.00 1.50 1.51 1.52 | 1.00 2.00 2.10 4.00 4.10
WINDOWS | WINDOWS NTkbprg
The information in this article applies to:
SUMMARYAn abstract base class is a class that contains one or more pure virtual functions. Attempting to use the IMPLEMENT_SERIAL() macro with an abstract base class results in the compiler error:
error C2259 : illegal attempt to instantiate abstract classWith Microsoft Visual C++ 32-bit edition, version 2.x, this compiler error is accompanied by an explanatory warning:
error C2259: cannot instantiate abstract class due to following members: warning C4259: pure virtual function was not definedTo work around this problem, implement the Serialize() function as normal for the abstract base class, and classes derived from the abstract base class, but skip over the abstract base class when using the IMPLEMENT_SERIAL() and DECLARE_SERIAL() macros.
MORE INFORMATIONThe IMPLEMENT_SERIAL() and DECLARE_SERIAL() macros declare a Construct() function, which creates an instance of the class. This function is used to create a new instance of the class when an object is read from a CArchive. Because C++ does not allow you to create instances of an abstract base class, the compiler error mentioned above is given when attempting to implement the construct function. However, because an object of the abstract base class cannot be created, it is not possible that such an object was ever stored in an archive. Only objects derived from the abstract base class need to be capable of serialization. To serialize a class derived from an abstract base class, do not use the IMPLEMENT_SERIAL() and DECLARE_SERIAL() macros with an abstract base class. Instead, just declare the Serialize() function and implement it as normal so that it will serialize the data in the abstract base class. When you derive classes from the abstract base class, use the IMPLEMENT_SERIAL() and DECLARE_SERIAL() macros. In the IMPLEMENT_SERIAL() macro, specify the base class of the abstract base class as the base class. In the Serialize() function of the derived class, serialize the data in the class and then call the base class version of Serialize() in the abstract base class so that data in the base class is serialized. For an example of the technique, please see the BLOCKS sample (S13483) in the Microsoft Software Library.
|
Additional reference words: kbinf 1.00 1.50 2.00 2.10 2.50 2.51 2.52 3.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |