First of all, very embarrassed, I am still looking at C Language Programming, but I haven't seen the chapter about RTTI. In addition, I also rarely use the characteristics of C RTTI. Therefore, the understanding of RTTI is limited to its own exploration and thinking. If not correct, please correct it.
The RTTI feature is one of the tight features of the C language. Compared to other languages (such as Java), the RTTI capability of C is very poor. This should have important relationships with C design requirements: performance. That's right, performance factors make C 's many places to be perfect, but because of this, in advanced generic language, only C can be compared with C performance.
1: Research on TypeID
In C , there seems to have only one thing related to RTTI, that is, Dynamic_cast, I think TypeID is part of RTTI, but my experiment shows that it is not. The operation of TypeID is determined during the compilation period. The following code can prove:
#include
Class a {};
Class B: Public a {};
INT main () {a * pa; b, * pb; pb = & b; PA = Pb; std :: cout << "name1: << (typeid (pa) .Name ()) <<" / tname2 : "<< (typeid (pb) .Name ()) << std :: endl;
Std :: cout << "PA == Pb:" << (TypeId (PA) == TypeId (PB)) << std :: end1; return 0;}
The TypeID cannot be discriminated at all PAs actually a B *. In other words, TypeID is explained by literally, don't expect it to recognize a void * actually INT * (this is also unique: P). In fact, the practical value is not large.
Of course, in some special places, it is also possible, such as templates.
Template
If the compiler is optimized, it will not produce waste code, because the TypeID compile period can be determined.
2: Dynamic_cast
Sorry, I just got the topic, I am impressed with Dynamic_cast, what is it realized? After some thinking, I think the easiest solution is to save the information in the vTable, which takes up a VTALBE table project. Experiments and books also prove this. But there will be a problem, what should I do without VTABLE? What should I do with the type of built? In fact, there is no VTABLE class, it doesn't need polymorphism, it doesn't need RTTI, and the built-in type is also the same. That is to say, Dynamic_cast only supports classes with virtual functions. Moreover, Dynamic_CAST cannot perform the conversion of non_base_class * to Class T *, such as Void * -> Class T *, because it cannot get VTABLE. In this way, the meaning and method of use of Dynamic_cast is very clear, it is to support polymorphism. It is used to achieve secure conversion from the base class to the derived class. At the same time, it also avoids the use of static_cast-unsafe type conversion in most cases.
3: Conclusion
Although the RTTI mechanism of C is simple, or simple, it makes static type conversion useless. This is also an indispensable mechanism of C . In the future, if C can provide an optional stronger RTTI mechanism, such a language can become more powerful as in Java. Of course, how to provide an illegal RTTI mechanism that does not lose performance is a topic worthy of in-depth research.