answersLogoWhite

0

What is RTTI?

Updated: 11/3/2022
User Avatar

Wiki User

19y ago

Best Answer

RTTI means run time type identification. It is the process of identifying the type of an identifier or an object during runtime which is not known during compile time. Through this, we can hide the implementation details of a particular identifier or an object at the user level during programming so that it ca be distinguished during runtime and used further.Object persistency is acheived through RTTI

User Avatar

Wiki User

19y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is RTTI?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is rtti in c plus plus programming?

RTTI is runtime type information. It is a basic requirement of the dynamic_cast operator in order to ensure all casts between polymorphic objects within a class hierarchy are done safely. Other cast operators such as safe_cast also utilise RTTI. However, RTTI has an overhead that can be detrimental to performance thus RTTI is often utilised in debug code to verify C-style casts in release code (such as reinterpret_cast) are always done safely. Both static_cast and reinterpret_cast are inherently unsafe, however static_cast does invoke user-defined conversions, whereas reinterpret_cast does not, and should only be used as a last resort. But try telling Microsoft that... In many cases, RTTI can be eliminated completely by utilising a well-designed class hierarchy with virtual interfaces that avoid the need to cast between object types. In other words, it should not be necessary for any base class to even be aware of any of its derived class' types (let alone their specialities) in order to use them. By calling virtual methods, casting between types is done safely and automatically, behind the scenes, by virtue of the virtual table. Moreover, a v-table has a much lesser overhead than RTTI. That said, there will be times when RTTI is the only solution to a problem, especially when working with legacy code and libraries for which you have little or no control over. If there's ever any doubt about what a pointer is really pointing at, use RTTI. Better safe than sorry.


What is the difference between null and void pointers?

A void pointer is a pointer that has no type information attached to it.A null pointer is a pointer that points to "nothing". A null pointer can be of any type (void included, of course).


What is meant by the data type of a variable in c plus plus?

The built-in (fundamental) data types are int, char, bool, float, double and pointer. All other integral types are either modified types or aliases (typedefs). User-defined types (including those provided by the standard library) are not considered part of the language.


How much a base class members can access of derived class?

Ideally, none. Base classes should have no knowledge whatsoever of their derived classes and therefore should have no access to any members declared in the derived classes. However, derived class methods are accessible via virtual functions declared in the base class. The correct methods (overrides) are called via the virtual table, but the base class itself requires no specific knowledge of the derived class in order to call these methods. This makes sense since a base class cannot know in advance all the classes that may or may not derive from it in the future. If a method must be guaranteed to be implemented by a derived class, a pure-virtual method can be declared in the base class instead. This renders the base class abstract, meaning you cannot instantiate an object from the base class, only from a derivative that fully implements (or inherits) all the pure-virtual methods. Conversely, a derived class can access all the public and protected members of its base class.


What is the difference between base class pointer and derived class pointer in c plus plus?

Your understanding of is a in C++ (polymorphism, in general) is wrong. A is B means A has at least the properties of B, possibly more, by definition. This is compatible with your statements that a Dog has a Pet and that [the attributes of] a Pet is[are] a subset of [attributes] of Dog. It's a matter of definition of polymorphism and inheritance. The diagrams you draw are aligned with the in-memory representation of instances of Pet and Dog, but are misleading in the way you interpret them. Pet* p = new Dog; The pointer p is defined to point to any Pet-compatible object, which in C++, is any subtype of Pet (Note: Pet is a subtype of itself by definition). The runtime is assured that, when the object behind p is accessed, it will contain whatever a Pet is expected to contain, and possibly more. The possibly more pat is what the Dog in your diagram is. The way you draw your diagram lends to a misleading interpretation. Think of the layout of class-specific members in memory: Pet: [pet data] Dog: [pet data][dog data] Cat: [pet data][cat data] Now, whenever Pet *p points to, is required to have the [pet data] part, and optionally, anything else. From the above listing, Pet *p may point to any of the three. As long you use Pet *p to access the objects, you may only access the [pet data], because you don't know what, if anything, is afterwards. It's a contract that says This is at least a Pet, maybe more. Whatever Dog *d points to, must have the [pet data] and [dog data]. So the only object in memory it may point to, above, is the dog. Conversely, through Dog *d, you may access both [pet data] and [dog data]. Similar for the Cat. Let's interpret the declarations you are confused about: Pet* p = new Dog; // [1] - allowed! Dog* d = new Pet; // [2] - not allowed without explicit casting! My understanding is that 1 should not be allowed without warnings because there is no way a pointer should be able to point to an object of its superset's type (Dog object is a superset of Pet) simply because Pet does not know anything about the new members that Dog might have declared (the Dog - Pet subset in the diagram above). The pointer p expects to find [pet data] at the location it points to. Since the right-hand-side is a Dog, and every Dog object has [pet data] in front of its [dog data], pointing to an object of type Dog is perfectly okay. The compiler doesn't know what else is behind the pointer, and this is why you cannot access [dog data] through p. The declaration is allowed because the presence of [pet data] can be guaranteed by the compiler at compile-time. (this statement is obviously simplified from reality, to fit your problem description) 1 is equivalent of an int* trying to point to a double object! There is no such subtype relationship between int and double, as is between Dog and Pet in C++. Try not to mix these into the discussion, because they are different: you cast between values of int and double ((int) double is explicit, (double) int is implicit), you cannot cast between pointers to them. Just forget this comparison. As to [2]: the declaration states "d points to an object that has [pet data] and [dog data], possibly more." But you are allocating only [pet data], so the compiler tells you you cannot do this. In fact, the compiler cannot guarantee whether this is okay and it refuses to compile. There are legitimate situations where the compiler refuses to compile, but you, the programmer, know better. That's what static_cast and dynamic_cast are for. The simplest example in our context is: d = p; // won't compile d = static_cast<Dog *>(p); // [3] d = dynamic_cast<Dog *>(p); // [4] [3] will succeed always and lead to possibly hard-to-track bugs if p is not really a Dog. [4] will will return NULL if p is not really a Dog. I warmly suggest trying these casts out to see what you get. You should get garbage for [dog data] from the static_cast and a NULL pointer for the dynamic_cast, assuming RTTI is enabled.

Related questions

What is rtti in c plus plus programming?

RTTI is runtime type information. It is a basic requirement of the dynamic_cast operator in order to ensure all casts between polymorphic objects within a class hierarchy are done safely. Other cast operators such as safe_cast also utilise RTTI. However, RTTI has an overhead that can be detrimental to performance thus RTTI is often utilised in debug code to verify C-style casts in release code (such as reinterpret_cast) are always done safely. Both static_cast and reinterpret_cast are inherently unsafe, however static_cast does invoke user-defined conversions, whereas reinterpret_cast does not, and should only be used as a last resort. But try telling Microsoft that... In many cases, RTTI can be eliminated completely by utilising a well-designed class hierarchy with virtual interfaces that avoid the need to cast between object types. In other words, it should not be necessary for any base class to even be aware of any of its derived class' types (let alone their specialities) in order to use them. By calling virtual methods, casting between types is done safely and automatically, behind the scenes, by virtue of the virtual table. Moreover, a v-table has a much lesser overhead than RTTI. That said, there will be times when RTTI is the only solution to a problem, especially when working with legacy code and libraries for which you have little or no control over. If there's ever any doubt about what a pointer is really pointing at, use RTTI. Better safe than sorry.


What is program written in c plus plus for RTTI?

RTTI (run-time type information) is often necessary when dealing with a hierarchy of classes for which the virtual interface does not provide enough information about the total object from the base class alone. The chief method of enabling RTTI is to use the dynamic_cast operator. This operator can cast pointers or references to base classes to pointers or references to derived classes. When casting to a pointer, a nullptr indicates the cast failed. When casting to a reference, a bad_cast exception is thrown (because references cannot be null). Thus before dynamically casting a reference, it pays to get some more information about the total object. This is achieved with the typeid operator. void foo (shape& s) { if (typeid (s) == typeid (circle)) { circle c = dynamic_cast<circle&> (s); // operate upon c... } } Note that RTTI is often misused. If it is impossible or undesirable to modify the base class to provide a suitable interface, then RTTI may be warranted. However, keep in mind that the problem is not that you are attempting to gain access to an interface that isn't available in the base class, it is that you are attempting to access that interface through the wrong class in the first place. Sometimes that is unavoidable, especially in user-interfaces where the application and system interact through widgets and controls where information can often become lost in the exchange. However, always try and look at alternatives whenever possible before resorting to RTTI as this can often indicate a poor design choice.


What is the difference between null and void pointers?

A void pointer is a pointer that has no type information attached to it.A null pointer is a pointer that points to "nothing". A null pointer can be of any type (void included, of course).


What is the history of c plus plus?

C++ was written by Bjarne Stroustrup at Bell Labs during 1983-1985. C++ is an extension of C. Prior to 1983, Bjarne Stroustrup added features to C and formed what he called "C with Classes". He had combined the Simula's use of classes and object-oriented features with the power and efficiency of C. The term C++ was first used in 1983. C++ was developed significantly after its first release.1 In particular, "ARM C++" added exceptions and templates, and ISO C++ added RTTI, namespaces, and a standard library.1 C++ was designed for the UNIX system environment. With C++ programmers could improve the quality of code they produced and reusable code was easier to write. Bjarne Stroustrup had studied in the doctoral program at the Computing Laboratory at Cambridge University prior to joining Bell Labs. Now, Bell Labs no longer has that name since part of Bell Labs became AT&T Labs. The other half became Lucent Bell labs. Prior to C++, C was a programming language developed at Bell Labs circa 1969-1973. The UNIX operating system was also being developed at Bell Labs at the same time. C was originally developed for and implemented on the UNIX operating system, on a PDP-11 computer by Dennis Ritchie. He extended the B language by adding types in 1971. He called this NB for New B. Ritchie credited some of his inspiration from theAlgol68 language. Ritchie restructured the language and rewrote the compiler and gave his new language the name "C" in 1972. 90% of UNIX was then written in C. The committee that wrote the 1989 ANSI Standard for C had started work on the C Standard project in 1983 after having been established by ANSI in that year. There were quite a number of versions of C at that time and a new Standard was necessary. C is portable, not tied to any particular hardware or operating system. C combines the elements of high-level languages with the functionality of assembly language and has occasionally been referred to as a middle-level computer language. C makes it easy to adapt software for one type of computer to another. C was a direct descendant of the language B. The language B was developed by Ken Thompson in 1970 for the new UNIX OS. B was a descendant of the language BCPL designed by Martin Richards, a Cambridge University student visiting MIT.


What is meant by the data type of a variable in c plus plus?

The built-in (fundamental) data types are int, char, bool, float, double and pointer. All other integral types are either modified types or aliases (typedefs). User-defined types (including those provided by the standard library) are not considered part of the language.


How much a base class members can access of derived class?

Ideally, none. Base classes should have no knowledge whatsoever of their derived classes and therefore should have no access to any members declared in the derived classes. However, derived class methods are accessible via virtual functions declared in the base class. The correct methods (overrides) are called via the virtual table, but the base class itself requires no specific knowledge of the derived class in order to call these methods. This makes sense since a base class cannot know in advance all the classes that may or may not derive from it in the future. If a method must be guaranteed to be implemented by a derived class, a pure-virtual method can be declared in the base class instead. This renders the base class abstract, meaning you cannot instantiate an object from the base class, only from a derivative that fully implements (or inherits) all the pure-virtual methods. Conversely, a derived class can access all the public and protected members of its base class.


What is the difference between base class pointer and derived class pointer in c plus plus?

Your understanding of is a in C++ (polymorphism, in general) is wrong. A is B means A has at least the properties of B, possibly more, by definition. This is compatible with your statements that a Dog has a Pet and that [the attributes of] a Pet is[are] a subset of [attributes] of Dog. It's a matter of definition of polymorphism and inheritance. The diagrams you draw are aligned with the in-memory representation of instances of Pet and Dog, but are misleading in the way you interpret them. Pet* p = new Dog; The pointer p is defined to point to any Pet-compatible object, which in C++, is any subtype of Pet (Note: Pet is a subtype of itself by definition). The runtime is assured that, when the object behind p is accessed, it will contain whatever a Pet is expected to contain, and possibly more. The possibly more pat is what the Dog in your diagram is. The way you draw your diagram lends to a misleading interpretation. Think of the layout of class-specific members in memory: Pet: [pet data] Dog: [pet data][dog data] Cat: [pet data][cat data] Now, whenever Pet *p points to, is required to have the [pet data] part, and optionally, anything else. From the above listing, Pet *p may point to any of the three. As long you use Pet *p to access the objects, you may only access the [pet data], because you don't know what, if anything, is afterwards. It's a contract that says This is at least a Pet, maybe more. Whatever Dog *d points to, must have the [pet data] and [dog data]. So the only object in memory it may point to, above, is the dog. Conversely, through Dog *d, you may access both [pet data] and [dog data]. Similar for the Cat. Let's interpret the declarations you are confused about: Pet* p = new Dog; // [1] - allowed! Dog* d = new Pet; // [2] - not allowed without explicit casting! My understanding is that 1 should not be allowed without warnings because there is no way a pointer should be able to point to an object of its superset's type (Dog object is a superset of Pet) simply because Pet does not know anything about the new members that Dog might have declared (the Dog - Pet subset in the diagram above). The pointer p expects to find [pet data] at the location it points to. Since the right-hand-side is a Dog, and every Dog object has [pet data] in front of its [dog data], pointing to an object of type Dog is perfectly okay. The compiler doesn't know what else is behind the pointer, and this is why you cannot access [dog data] through p. The declaration is allowed because the presence of [pet data] can be guaranteed by the compiler at compile-time. (this statement is obviously simplified from reality, to fit your problem description) 1 is equivalent of an int* trying to point to a double object! There is no such subtype relationship between int and double, as is between Dog and Pet in C++. Try not to mix these into the discussion, because they are different: you cast between values of int and double ((int) double is explicit, (double) int is implicit), you cannot cast between pointers to them. Just forget this comparison. As to [2]: the declaration states "d points to an object that has [pet data] and [dog data], possibly more." But you are allocating only [pet data], so the compiler tells you you cannot do this. In fact, the compiler cannot guarantee whether this is okay and it refuses to compile. There are legitimate situations where the compiler refuses to compile, but you, the programmer, know better. That's what static_cast and dynamic_cast are for. The simplest example in our context is: d = p; // won't compile d = static_cast<Dog *>(p); // [3] d = dynamic_cast<Dog *>(p); // [4] [3] will succeed always and lead to possibly hard-to-track bugs if p is not really a Dog. [4] will will return NULL if p is not really a Dog. I warmly suggest trying these casts out to see what you get. You should get garbage for [dog data] from the static_cast and a NULL pointer for the dynamic_cast, assuming RTTI is enabled.


Why do you use virtual functions in C plus plus?

Virtual means that decision would be taken at another stage... first base class constructor is called and construction of object cannot be held anonymous. that's y virtual constructors are not possible.AnswerOne reason is that a constructor cannot be virtual is because it is the constructor's job to fill in the v-table for virtual functions. If a constructor was allowed to be virtual, then you end up with kind of a chicken and egg paradox. In order to call the constructor, the compiler would have to look in the v-table to find where the constructor is located. Unfortunately, it's the constructor's job to place the address of virtual functions (which would include itself) into the v-table. Without RTTI, it would be impossible to resolve which child's constructor to call. In most cases, a virtual helper function that the constructor calls is more then enough.