How to create object in runtime.

Hi,

How can I create object using the type information. In MFC, if you declare a class use the DECLARE_DYNCREATE, then you can can create a object like
pRuntimeClassObj->CreateObject(). how to do in OCC.

thanks.

fhchina's picture

Hi,

I couldn't find any RTTI available in OCC for creating object, the following is the DEFINE_STANDARD_RTTI macro:

#define DEFINE_STANDARD_RTTI(C1) \
Standard_EXPORT friend Handle_Standard_Type& C1##_Type_(); \
Standard_EXPORT const Handle(Standard_Type)& DynamicType() const ; \
Standard_EXPORT Standard_Boolean IsKind(const Handle(Standard_Type)&) const ;

No any structure available for danamic creation.

But you can do it yourself. I write a very simple one: you can easily write your own.

#define DECLARE_DYNAMIC(classname)\
Handle_Standard_Transient yourCreate##classname();

#define IMPLEMENT_DYNAMIC(classname)\
Handle_Standard_Transient yourCreate##classname()\
{ return new classname; }

#define RUNTIME_CREATE(classname) yourCreate##classname()

of course, there must be a default constructor with classname. If you want to create a class by its string name as in Java, you can maintain a hash table to deal with it.

What I am most interested is: which application context in OCC is need this mechanism?
Can you tell me your design idea?

fhchina

Lugi.C's picture

Hi,

Yeah, your idea is good, but the the name of your macro is same as MFC's:-)

My idea of doing so is that I can create my object according to some other information, such as an id. I register all of the id with an runtime object such as Standard_Integer with an Standard_Type, then in future in my code, I will call like this:
getRuntimeObject()->CreateObject();

regards

Lugi.C

fhchina's picture

Hi,

Sorry, I forget we are both using MFC. :-)

regards,
fhchina