From lechner@cs.uml.edu Wed Sep 20 22:09:30 2006 Subject: Fox alt to bde hasType: Check subtype by dynamic cast To: lechner@cs.uml.edu (Bob Lechner) RJLRef: $PH/COOL-BDE/bdegen13/hasTypeAlternative_C++DynamicCast.txt > > This may be useful on a bde graphobject display (List) class item, > to see if it is in the graph, node, caption or hlink subclass. > > Right now bde/src uses macro hasType(id,XX) below. > (267 calls or comments, including assert(hasType(...) checks.) > This is analogous to fox's isMemberOf(...) below. > > hasType is application-independent, like dprint: > ------ > mercury(16)> grep '#define hasType' ../pr_util_nolog/9*.h > 792:#define hasType(key, ttabbr) \ > ((&key)?(strstr(decode_retstr(&key),#ttabbr)?1:0):0) > -------- > decode_retstr looks up a ttabbr at an index and does a string compare. > But (&key)==encode(ttabbr) is slower since encode serialy > searches for ttabbr in a list of string values. > > > The polymorphic list at topobject->subobjects[->next...] > was never dis-aggregated into subtype lists. > The pr_loaded VMNetDB segregates types in bde's component hierarchy. > This is more faithful to bde's [relational] data model. > > R Lechner > > Forwarded message: > >> > From foxgui-users-bounces@lists.sourceforge.net Tue Sep 19 22:16:47 2006 >> > To: foxgui-users@lists.sourceforge.net >> > Cc: The Devils Jester >> > Subject: Re: [Foxgui-users] Check what type a widget is? >> > >> > On Tuesday 19 September 2006 19:57, The Devils Jester wrote: >> > > What is the proper method of checking to see what class a specific widget= >> > is? >> > > (I.E. I need to determine if its a button or a toggle button, etc...) >> > >> > >> > Even so, for general reference: >> > >> > if(window->isMemberOf(FXMETACLASS(ClassName))){ >> > ... >> > } >> > >> > This would work; however, the C++ way is better: >> > >> > ClassName *someclass=dynamic_cast(window); >> > if(someclass){ >> > ... >> > } >> > >> > >> > This is language-supported, and since you usually want to know >> > what class something is in order to cast the pointer, it kills >> > two birds with one stone, so to speak. >> > >> > Apart from that, a C++ style cast takes care of potential multiple- >> > inheritance issues [not that this is relevant inside FOX itself, >> > which is strictly single-inheritance; however it may be an issue >> > in your own classes]. >> > >> > - Jeroen >> > > Message 3/1369 From Jeroen van der Zijp Sep 20, 06 08:17:46 PM -0500 > Cc: The Devils Jester > Subject: Re: [Foxgui-users] Check what type a widget is? > On Wednesday 20 September 2006 17:23, The Devils Jester wrote: > > So in the C++ example you gave, ClassName would be say FXLabel, and > > if the passed object 'window' has FXLabel in its inheritance history, > > the new pointer 'someclass' will be a non null value? Sorry I am a > > bit green as far as OOP goes. > > C++ has added dynamic_cast<> as a checked downcast specifically for these > situations. It only requires that the object have a vtable [which FXObject > and its derivatives do]. > > The other one is static_cast<>, which you can deploy if you KNOW that > the cast will be successful [it doesn't check, like dynamic_cast does, > thus its faster]. > > A C++ object which inherits from more than one base class comprises > all of its base classes in its struct. When you "cut" it down to > one of its bases, the C++ compiler knows the type and can add the > necessary struct-offsets. > > However, when you down-cast, with the C-style cast you'd just be > reinterpreting the pointer to this as a pointer to that, and thus > in case of multiple inheritance this will be total garbage. > > So its *highly* recommended to use the new C++ style casts when navigating > a class hierarchy, and when the class-hierarchy uses M.I. it is *essential*. > > Regards, > > - Jeroen