0

DECLARE_DYNAMIC() is used for getting RTTI, when a class derived from CObject class, I know it. But i saw in a code,a class derived from CPropertySheet have this MACRO. -> what is the need of it, as neither iskindof() nor any other Run time information is used?

this derived class has two constructors , called via new when creating objects.

my question is What is the need of DECLARE_DYNAMIC() ,in this class ?

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77

1 Answers1

2

The use of this macro allows easy type checking in the MFC way. Assume you have a special CPropertyPage base class, Or you have a special derived CPropertySheet with a special interface named CMyClass you can easily do a cast with a type check with DYNAMIC_DOWNCAST.

So the fast answer is: If you want to use DYNAMIC_DOWNCAST or STATIC_DOWNCAST with the MFC type checking. It just allows the usage of IsKindOf.

It is the lowest form of the tripple DECLARE_DYNAMIC, DECLARE_DYNCREATE, DECLARE_SERIAL.

Also there are more answers on this here in stack overflow.

Community
  • 1
  • 1
xMRi
  • 14,982
  • 3
  • 26
  • 59
  • I am sure that is true. Nevertheless, why not add this macro to every class derived from CObject? It is an overhead that's why. Much better is to ask, which parts of the MFC require these macros? Document, View & Framework require at least DYNCREATE, with possibly Document SERIAL if archiving. Does PropertyPage require DYNAMIC? Don't know. This is not an academic question as I am working with PropertyPage's – SJHowe Sep 11 '19 at 13:47
  • No property pages don't need it. DECLARE_DYNAMIC is only required, where classes need to be created on the fly with a given CRuntimeCLas object. – xMRi Sep 12 '19 at 13:14