quote:
...I was commenting on the fact that his object actual IS a base, and he's trying to cast it to a derived.
Oh, NEVER do that - it's the devil!
I have to agree, I avoid RTTI like the plague as well. Since I'm into the COM thing I rely on QI for that functionality. I resisted this philosophy for a while, but change my mind when I ran into IMoniker. I wanted to create a derivation of an MFC CComboBox to work with some DirectShow filters, so the user could have a look and pick one. I ended up making the ComboBox list IMoniker's, we means it can used a number of other COM based components besides DirectShow filters. But in order to get useful information out of the ComboBox, you would need to perform a RTTI type dynamic cast on the IMoniker pointer if it weren't for QI. Since all the filters expose an IMoniker interface you can use them with anything that can manipulate monikers. Once the user picks a moniker from the list, you can query it for an interface that actually does some work (IBaseFilter in my case). This means anything that exposes IMoniker works with my ComboBox. Since the application must know what the original interface is, it knows which interface to ask for once a IMoniker comes it's way. Once it settled on me how useful that was, I started liking run-time casting (beit RTTI or QI) alot more.
quote:
I thought conditional if chains were something that inheritence was created to AVOID.
Exactly how I felt - what's the point in fracturing everything into interfaces and classes and then dumping into a switch to decide how to cast and what to do. I thought that you ought to have a virtual method that everyone shares to do that work to make good use of polymorphism. What really ought to be done, is everyone that wants to use a certain service, ought to implement the inferface required to use it (e.g. implement IMoniker to use services that require names, like a MonikerComboBox). So within a service you should should use polymorphism via the virtuals. To hook-up and detach from services, you should use RTTI/QI to navigate the available interfaces.
- Magmai Kai Holmlor
Hark, I have found the Holy Grail!
...
class base{}class child : public base{}//code somewherebase* obj = new base();child* objChild = static_cast<Child*>(obj); //What happens here?
A obj is copied to objChild, their values will be the same. It will even work correctly since it's a SI (single inheritence) branch. The compiler will about it, but you force the cast using static_cast (reinterpret_cast will also work, but static is a safer). Normally you use dynamic_cast to move up the inheritence tree; dynamic_cast will move down and around the tree (like your code example above) if you enable RTTI. Using RTTI is safer and consequentially the preferred method. However, it is not _required for the above example. (i.e. obj==objChild)
If child inherited from more than one class (e.g. base1 and base2), _then it would be impossible to do that cast without some sort of RTTI. You could go from child to base or from child to base2 without RTTI, and it would offset the child pointer to where the base1 and base2 begin (i.e. base1!=base2).
Edited by - Magmai Kai Holmlor on October 17, 2001 6:24:29 PM