StackOverflow. My first post here.
I’ve come to D from C++ and MFC, which i’m using in my job – not only GUI stuff, but many MFC’s macro(DECLARE_DYNCREATE, etc.) and CObject class.
How i saw, in D we have std.Object class with factory method.
So, how to correctly rewrite this C++ code in D? if it is possible, of course.
class CPerson : public CObject
{
DECLARE_DYNAMIC( CPerson )
// other declarations
};
IMPLEMENT_DYNAMIC( CPerson, CObject )
void DoSmthWithObject(const CObject* pObj)
{
CPerson* pPerson = dynamic_cast<CPerson*>(pObj);
ASSERT_VALID(pPerson);
// Work with out CPerson object.
}
// Somethere in code create our CObject...
CObject* pMyObject = new CPerson;
// .. and do some strange things with it.
DoSmthWithObject(pMyObject);
Downcasts are already runtime-checked in D. Attempting to perform an invalid downcast will result in a null reference.