2

I've been developing for some time. And these beasts appear from time to time in MFC, wxWidgets code, and yet I can't find any info on what they do exactly.

As I understand, they appeared before dynamic_cast was integrated into core C++. And the purpose, is to allow for object creation on the fly, and runtime dynamic casts.

But this is where all the information I found, ends.

I've run into some sample code that uses DECLARE_DYNAMIC_CLASS and IMPLEMENT_DYNAMIC_CLASS within a DLL, and that is used for exported classes. And this structure confuses me.

Why is it done this way? Is that a plugin based approach, where you call LoadLibrary and then call the CreateDynamicClass to get a pointer which can be casted to the needed type?

Does the DECLARE/IMPLEMENT_DYNAMIC work over DLL boundaries? Since even class is not so safe to DLLEXPORT, and here we have a custom RTTI table in addition to existing problems.

Is it possible to derive my class from a DYNAMIC_CLASS from another DLL, how would it work?

Can anyone please explain me what these things are for, or where I can find more than a two sentences on a topic?

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
Coder
  • 3,695
  • 7
  • 27
  • 42
  • You may find this answer useful: http://stackoverflow.com/questions/14318993/why-is-declare-dynamic-implement-dynamic-nessary-for-dynamic-downcast/14319634#14319634 – snowdude Mar 04 '13 at 12:10

2 Answers2

2

This stuff appends addional type information to your class, which allows to RTTI in runtime-independent manner, possibility of having factories to create your classes and many other things. You can find similar approach at COM, QMetaObject, etc

kassak
  • 3,974
  • 1
  • 25
  • 36
  • Does it mix with cross module approaches? If the objects are declared in one module, and used in another? Where is the type lookup table stored in general? – Coder Mar 04 '13 at 23:40
  • As said in http://docs.wxwidgets.org/trunk/overview_rtti.html it is stored as linked list of static `wxClassInfo` instances. It is safe to use that across modules boundaries. But safeness of using that classes depends on their crossmodule safeness, just like in COM. `QueryInterface` is COM analogue of `dynamic_cast`, but if your interface uses classes like `std::vector`(memory layout may differ at Debug/Release/Runtime ver) their usage is unasafe – kassak Mar 05 '13 at 12:55
0

Have you looked at the definitions of DECLARE/IMPLEMENT_DYNAMIC?

In the MS world, all uppercase usually denotes a macro, so you can just look up the definition and try to work out what it's doing from there. If you're in Visual Studio, there's a key you can hit to jump to the definition - see what it says, and look that up and try to work from there.

Michael Kohne
  • 11,888
  • 3
  • 47
  • 79