C + + RTTI

Source: Internet
Author: User

RTTI (run-time type information, through run-time type information) programs can use pointers or references to base classes to examine the actual derived types of the objects referred to by these pointers or references.

C + + has three elements that support Rtti
1) If possible, the dynamic_cast operator will use a pointer to the base class to generate a pointer to the derived class, otherwise the operator returns a 0--null pointer.
dynamic_cast < Type *> (PT);
In general, if the type that points to an object (*PT) is type or is derived directly or indirectly from type, the above expression converts the pointer pt to a pointer of type.
Otherwise, the result is 0, which is the null pointer.
2) The typeid operator returns I a value that indicates the type of the object.
The typeID object makes it possible to determine whether two objects are of the same type, which is somewhat similar to sizeof and accepts two parameters:

class name
The result is an expression of the object
It returns a reference to the Type_info object.
3) The TYPE_INFO structure stores information about a particular type.
Type_info is a class defined in header file TypeInfo that overloads the = = and! = operator

#include <iostream>#include <cstdlib>#include <ctime>#include <typeinfo>using STD::cout;classgrand{Private:intHold Public: Grand (inth =0): Hold (h) {}Virtual voidSpeak ()Const{cout<<"I am a grand class!\n";}intValue ()Const{returnHold }};classSuperb: Publicgrand{ Public: Superb (inth =0): Grand (h) {}voidSpeak ()Const{cout<<"I am a superb class!! \ n "; }Virtual voidSay ()Const{cout<<"I hold the superb value of"<< Value () <<"!\n"; }};classMagnificent: Publicsuperb{Private:CharCh Public: Magnificent (inth =0,Charc =' A '): Superb (h), CH (c) {}voidSpeak ()Const{cout<<"I am a magnificent class!!! \ n "; }voidSay ()Const{cout<<"I hold the character"<< CH <<"and the integer"<< Value () <<"!\n"; }}; grand* GetOne ();intMain () {STD:: Srand (STD:: Time (0));    Grand* pg; superb* PS; for(inti =0; I <5;        i++) {pg = GetOne (); Pg->speak ();if*P1 =dynamic_cast<superb *> (PG)) Ps->say ();if(typeID(magnificent) = =typeID(*PG))cout<<"Yes, you ' re really magnificent.\n"; }return 0;}//Randomly create three kinds of objects in a category, initialize them, and thenThe //address is returned as a grand* pointer. grand* GetOne () {grand* p;Switch(STD:: Rand ()%3)    { Case 0: p =NewGrand (STD:: Rand ()% -); Break; Case 1: p =NewSuperb (STD:: Rand ()% -); Break; Case 2: p =NewMagnificent (STD:: Rand ()% -,' A '+STD:: Rand ()% -); Break; }returnP;}

Operation Result:

The rtti of C + + provides several basic functions:
1, safe downcast, when you use dynamic_cast dynamic from the base class pointer to the child class pointer, Rtti can guarantee the safe conversion, if the type does not match throws a Bad_cast exception.
2. Dynamically gets the class name of an object. You can use typeID to get a Type_info object that holds the most basic information of this type, such as the name.
3, traversal on the inheritance tree, using the Type_info object obtained by typeID provides a before method that can be used to find the base class of this type.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + RTTI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.