Is it guaranteed in all c++ runtimes to give a valid value if the class has at least one virtual method? What if one of its base classes has a virtual method but it overrides it without the virtual keyword?
Is it guaranteed in all c++ runtimes to give a valid value if the class has at least one virtual method? What if one of its base classes has a virtual method but it overrides it without the virtual keyword?
It is no more guaranteed to work than any other aspect of C++. Compiler vendors are weird, but if they claim conformance then 'typeid' will probably behave in such a manor. Realize though that what 'typeid' "returns" may or may not be valid even between different runs of the same program.Is it guaranteed in all c++ runtimes to give a valid value if the class has at least one virtual method?
Using the 'virtual' keyword to override a virtual method in a derived class is strictly notational for the sake of the programmer.What if one of its base classes has a virtual method but it overrides it without the virtual keyword?
Soma
In answer the the first, it is not guaranteed, because RTTI can be turned off at compile time.
In answer to the second, an overridden virtual method is always virtual, even if the keyword "virtual" is left out. The presence of the keyword in derived classes serves only as a reminder to the programmer that the method is (still) virtual.
The restrictions are, if you're using it, it's more than likely that your design is buggered.
typeid will always give a valid value, unless your compiler is buggy. That doesn't necessarily mean it's a correct value, though. It depends on your view of "correct".
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law