Implementing Virtuals Via Statics
Since the virtual method
isA() returns a “runtime unique”
RWClassID, we can use this one virtual method to provide an index into a lookup table where various data or function pointers are stored. (This may remind you of C++ built-in
vtables!) Since
RWCollectables already depend on the existence of a single
RWFactory, we chose to use that
RWFactory instance to hold the lookup information.
The static method:
RWStringID RWCollectable::stringID(RWClassID id);
will attempt to look up
id in the
RWFactory instance. If it succeeds in finding an associated
RWStringID, it will return it. Otherwise, it will return
RWStringID("NoID").
The static method:
RWClassID RWCollectable::classID(RWStringID sid)
works in an analogous manner, looking in the
RWFactory instance to see if there is an
RWClassID associated with
sid. If the method finds one, it returns it; otherwise, it returns
RWClassID __RWUNKNOWN.