Organize the last installment of this series of notes about Run-time type information (RTTI). ensure a safe downward transition
Downward type conversion: refers to the type conversion from the parent class to the subclass, because the parent class information is less than the subclass, so the conversion is not safe in general without special mechanism support, and to support this mechanism requires an additional burden in both space and time:
1. Space requires additional space to store type information, usually a pointer to a type information node.
2. In time, additional time is required and the type of execution period has been determined.
One mainstream strategy is to differentiate class by declaring a virtual function, which reduces the extra burden by depositing a pointer to a class-related run-time information into a virtual function table. ensure the dynamic transformation of security
That is, the behavior of dynamic_cast<>: If the downward transition is safe, return the appropriate conversion type. If it is not secure, it returns empty.
For the following code:
base* PB = new Derived;
derived* PD = dynamic_cast<derived*> (pb);
For Dynamic_cast<>, the cost comes from:
1. A type descriptor for devired will be generated by the compiler.
2. b The type descriptor of the class object to which it points is to be obtained by the virtual table pointer at run time, as follows:
((type_info*) (pb->vptr[0])->_type_descriptor;
Two type descriptors need to be compared and may be handed over to a library function, which is obviously much more expensive than static_cast<>, but also much more secure. reference is not a pointer
When you apply a dynamic_cast<> to a pointer, you can produce two results:
1. Return the real address, this means that in the premise of security, the conversion succeeds, the dynamic type of this object is confirmed, some type-related operations can now be implemented on it.
2. Return 0, indicating that no object has been pointed to, indicating that it cannot be safely transformed.
2nd is not applicable for citation, because a reference cannot point to null, a reference is an alias to an object, and if a reference can be set to 0, then 0 is implicitly converted to a temporary object of that type, and the reference becomes an alias (alias) of that temporary object.
So when the dynamic_cast<> operator is applied to a reference, it is not possible to provide a return non-empty or empty equivalent to the pointer, but instead the following occurs:
1. If a safe transition is possible, it is similar to the pointer situation.
2. If the reference is not really a seed class, the Bad_cast exception is thrown because it cannot be returned to 0.
For example: For pointers I can define a transformation handler like this:
Simplify_conv_op (base* pb)
{
derived* pd = dynamic_cast<derived*> (pb)
if (PD)
{
// ... process PD
}
else
{//PD is
null which means cast fails.
}
}
For a reference, it cannot be judged by the null and the Non-empty:
Simplify_conv_op_ref (const base& B)
{
try {
derived& d = dynamic_cast<derived& > (b)
//...process D
}
catch (Bad_cast)
{
//catch bad_cast exception which means cast fails .
}
}
typeid operator
By typeID, you can determine the type information in advance by a reference and decide whether to perform dynamic type conversion:
Simplify_conv_op_ref (const base& B)
{
if (typeid (b) = = typeID (Derived))
{
derived& d = Dynamic_cast<derived&> (b)
//...process D
}
else ()
{
//...
}
}
The typeid operator returns a constant reference (const reference) of type type_info, and the possible declarations of the Type_info class are as follows:
Class Type_info {public
:
virtual ~type_info ();
BOOL operator== (const type_info&) const;
BOOL Operator!= (const type_info&) const;
BOOL before (const type_info&) const;
Const char* Name () const;//original class name
private:
//prevent memberwise init and copy
Type_info ( const type_info&);
type_info& operator= (const type_info&);
data member
};
The whole of the C + + object model of the notes are all over, counted on the total of 19 articles, many are also a lot of content, hope to be able to help colleagues, of course, it is important to give yourself a back-up, in order to restore after the use.