RE: [GD-General] Multiple Inheritance and RTTI
Brought to you by:
vexxed72
From: Wayne C. <wc...@re...> - 2003-02-04 11:23:28
|
> Some say it's lightning fast, others that it's appallingly slow. > I've never used it heavily enough to notice a hit. This depends on how you look at it and how much you're using it :) Compared to, say, just copying a pointer it is appallingly slow and if it's used a considerable amount the costs can begin to mount up. It's the RTTI that generally bloats code, all the type information has to go somewhere. Although if you start deriving from virtual base classes that can add a bit more (I've seen implementations where the processor spent a *lot* of time inside the RTTI\dynamic_cast code). Just make sure you profile regularly and have backup plans if it goes wrong. > The next most simple approach seems to be for the Entity base class to > provide a virtual accessor function for each interface, which returns NULL > by default but can be overriden by a derived class: > > One major drawback here seems to be that the vtable for each Entity class > will be bloated by over a hundred bytes, which seems like a bad move for > cache limited situations. I quite like this method, I'm not so worried by the vtable size, though. But more worried by the fact that you're limited to the supplied methods. So if you add a new class and it needs to be supported, then it needs to be added to the base class (meaning a *huge* recompile). Which limits the extensibility of the implementation. > Finally, the Entity base class could provide a single virtual accessor > function that takes an interface identifier as a parameter: This is the method I currently use, it's also how COM works. The only change from your example is that I pass in a pointer-to-a-pointer and the call returns a success\failure result. Eg IRenderable *pRenderable; If ( entity->QueryInterface( IID_IRenderable, ( void** )&pRenderable ) ) pRenderable->Render(); I usually implement the function as conditional code and to avoid the implementation pitfalls I use some macros to make it easy to implement. QueryInterface on the implementation side looks like: IMPLEMENT_CLASS( MyRenderableEntity ) IMPLEMENTS_INTERFACE( IEntity ) IMPLEMENTS_INTERFACE( IRenderable ) END_CLASS() A more in-depth description of this can be found in my (currently incomplete) SDK documentation located at http://www.btinternet.com/~nfactorial/MantraSDK.zip. Wayney -Virus scanned and cleared ok |