Imagine each class as if it had this virtual method, but only if it already has one other virtual object, and one object is created for each type:
extern std::type_info __Example_info; struct Example { virtual std::type_info const& __typeid() const { return __Example_info; } };
Then imagine that any use of typeid for a typeid(obj) object becomes obj.__typeid() . Similarly, using in pointers becomes pointer->__typeid() . With the exception of using null pointers (which calls bad_typeid), the case of a pointer is identical to the case without a pointer after dereferencing, and I will not mention it further. When applied directly to a type, imagine that the compiler inserts a link directly to the required object: typeid(Example) becomes __Example_info .
If the class does not have RTTI (i.e. it does not have virtual machines, for example NoRTTI below), then imagine it using the identical __ typeid method, which is not virtual. This allows a similar conversion to method calls, as described above, depending on the virtual or non-virtual sending of these methods; it also allows you to turn invocations of virtual methods into non-virtual dispatch, as can be done for any virtual method.
struct NoRTTI {};
Here, typeid must use RTTI for both parameters (B may be the base class for the later type), but RTTI is not needed for the local variable, because the dynamic type (or "run-time type") is absolutely known. This coincidence is no coincidence how virtual calls can avoid virtual sending.
struct StillNoRTTI : NoRTTI {}; void typeid_without_rtti(NoRTTI &obj) { typeid(obj); StillNoRTTI derived; typeid(derived); NoRTTI &ref = derived; typeid(ref);
Here using either obj or ref will match NoRTTI! This is true, although the former may have a derived class (obj may indeed be an instance of A or B), and although ref is definitely a derived class. All other functions (the last line of the function) will also be allowed statically.
Note that in these example functions, each typeid uses RTTI or not, as the name of the function implies. (Consequently, with_rtti is used in the comments.)
Fred nurk
source share