Template class and CObject's RTTI
Peter Chu HKMSUG -- peterchu@hkmsug.org
Friday, June 07, 1996
Environment: VC++ 4.0
Is it possible to derive a template class from CObject which
still use CObject's run time type information macros like
DECLARE_DYNAMIC() and IMPLEMENT_DYNAMIC()?
Thanks for your time.
Regards,
Peter
Mario Contestabile -- Mario_Contestabile.UOS__MTL@UOSMTL2.universal.com
Monday, June 10, 1996
>Is it possible to derive a template class from CObject which
>still use CObject's run time type information macros like
>DECLARE_DYNAMIC() and IMPLEMENT_DYNAMIC()?
>Thanks for your time.
>Regards,
>Peter
Deriving a non-template class from CObject
Your class may inherit from CObject if it's a template or not.
If it is not a template, the xxx_DYNAMIC() macros will function
normally (provided you inherit from only 1 CObject base class).
Eg:
class MyObject : public STLL, public MFCL{
DECLARE_DYNAMIC(MyList)
} MySplitObject;
ASSERT(MySplitObject.IsKindOf(RUNTIME_CLASS(MyList)));
CRuntimeClass *prt = MySplitObject.GetRuntimeClass();
// output is: "MyList"
cout << "MySplitObject's CRunTimeClass name is: " << prt->m_lpszClassName <<
endl;
Here, MyObject is a non-template class inheriting from CList and STL's list
templates.
If you enable RTTI during compilation and #include, you may even use
RTTI functions on MySplitObject.
Eg:
// output is: "class MyList"
cout << "MySplitObject's typeinfo.name() is: " << typeid(MySplitObject).name()
<< endl;
//Note: typeinfo.name() will leak memory (MSVC 4.1)
Deriving a template class from CObject
You can make your template class inherit from CObject, but there isn't an easy
way of making the xxx_DYNAMIC() macros to. Consider:
template
class Baker : public CObject {
DECLARE_DYNAMIC(Baker)
};
IMPLEMENT_DYNAMIC(Baker, CObject)
Will generate error:
"static struct CRuntimeClass Baker::classBaker' : member function not
declared in 'Baker"
In this case using RTTI without the macros would seem more appropriate.
So if you must use the xxx_DYNAMIC() macros, inherit a non template
class from CObject. If a template class must inherit from CObject use RTTI.
mcontest@universal.com
Roger Onslow -- Roger_Onslow@compsys.com.au
Wednesday, June 12, 1996
>Environment: VC++ 4.0
>
>Is it possible to derive a template class from CObject which
>still use CObject's run time type information macros like
>DECLARE_DYNAMIC() and IMPLEMENT_DYNAMIC()?
>
>Thanks for your time.
No and Yes...
NO:
It's one thing to include DECLARE_DYNAMIC()
(which I believe will generate compile errors anyway)
but there is no where to put IMPLEMENT_DYNAMIC()
for a template class, even if it did somehow work
YES:
derive you template class without the DECLARE etc.
then derive a concrete class from the template
and include the DECLARE_DYNAMIC() and
IMPLEMENT_DYNAMIC() in that class
eg.
class CMyObjectTemplate : public CObject {
...
};
class CMyObject : public CMyObjectTemplate {
DECLARE_DYNAMIC(CMyObject);
...
};
IMPLEMENT_DYNAMIC(CMyObject,CObject);
NOTE: Replace CObject with whatever CObject-derived
MFC class you require (eg CDialog)
This is not as nice as really encapsulating it in a
template but is a good compromise.
PS: Of course, you can still implement Serialize in
your template class
Roger Onslow
| Вернуться в корень Архива
|