|
-
May 25th, 1999, 06:09 PM
#1
DECLARE_DYNCREATE and pure vritual
Hi,
Is there any way one can have a class that implements "DYNCREATE" and also has a pure virtual function?
Is there a work around?
Thanks in advance
Regards
Venkatesh
-
May 25th, 1999, 08:53 PM
#2
Re: DECLARE_DYNCREATE and pure vritual
The reason an abstract class (that is, a class with at least one virtual function) won't work with DYNCREATE is that DECLARE_DYNCREATE contains the declaration:
static CObject* CreateObject();
which IMPLEMENT_DYNCREATE implements as:
CObject* PASCAL CMyClass::CreateObject()
{
return new CMyClass;
}
Now, of course, new CMyClass
fails for an abstract base class. I believe that these lines of code are the only differences between the DYNCREATE and the DYNAMIC macros -- is there any reason you couldn't simply use the DYNAMIC macros?
In Shepherd and Wingo's excellent "MFC Internals" (p. 173), they discuss abstract base classes in the context of the SERIAL macros and suggest redefining the IMPLEMENT_SERIAL macro as follows to accommodate abstract base classes:
#define IMPLEMENT_SERIAL_ABC(class_name, base_class_name, wSchema) \
_IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, NULL) \
CArchive& AFXAPI operator>>(CArchive& ar class_name* & pOb) \
{ pOb = (class_name*) ar.ReadObject(RUNTIME_CLASS(class_name));\
return ar; }
This new "ABC" (abstract base class) version of the macro eliminates the CreateObject() reference in the _IMPLEMENT_RUNTIMECLASS macro (replacing it with NULL) so that the constructor call for the abstract base class is never made.
Hope this helps.
-
May 25th, 1999, 09:04 PM
#3
Re: DECLARE_DYNCREATE and pure vritual
In the first line of test I omitted the word "pure" -- an abstract base class is a class with at least one pure virtual function.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|