Article View: comp.std.c++
Article #1928Re: Add dynamic_cast<type_info&>
From: Etay Bogner
Date: Fri, 08 Sep 1995 21:35
Date: Fri, 08 Sep 1995 21:35
94 lines
3778 bytes
3778 bytes
In article <42nsuv$bqf@engnews2.Eng.Sun.COM>, clamage@Eng wrote:
>> I don't see what problem you are trying to solve. The expression
>> typeid(p)
>> returns a 'const type_info&'. If given a pointer to a polymorphic type,
>> it returns the type of the actual object; otherwise it returns the static
>> type of the expression. What programs are made simpler by giving special-
>> case semantics to dynamic_cast instead of just writing 'typeid', which seems
>> to be what you want anyway?
When you write an object IO mechanism, you encounter the following problem :
When you READ ( LOAD ) an object from the data base, what type will you RETURN ?
There are several solutions to object IO, and I have another one too, but
the problem is the same.
A natural interface to such a mechanism is ( from the USER prespective ) :
try {
A* a;
Load(a); // here I have a nicer interface, but that's not the issue
}
catch (bad_cast& cast) {
}
meaning that the user THINKS that now he would get an A ( or something
derived from it ), or in other words "GIVE ME THE NEXT A in the data
base", and if not he would get a "bad_cast" exception, for example.
Now, how would you write such a template ( for Load ):
void* readNextObjectFromDataBase(); // this is actually something like a
virtual function
template <class T>
void Load(T*& t) {
t = dynamic_cast<T*>(readNextObjectFromDataBase()); // this is NOT accepted
}
It must be clear that "readNextObjectFromDataBase()" has a pointer to the
complete object and of course that it has it's type id.
So the question is "how to cast that void* to T* ?".
Facts :
1) The objects are not derived from a common base.
2) "readNextObjectFromDataBase()" has all the information to do the dynamic_cast
PER type. Again ,think of it as a virtual function.
3) "readNextObjectFromDataBase()" must have the SAME interface for all
types ( and that's not new ).
4) So, readNextObjectFromDataBase() can return void* only or a pair<const
type_info&, void*>.
you can't write in Load the dynamic cast from a void*, so you can't write Load
5) my solution : re-write "readNextObjectFromDataBase()" so it will accept
a 'const type_info&' so it CAN write the dynamic_cast and return a void*
to T*.
so that becomes :
void* readNextObjectFromDataBase(const type_info&); // this is actually
something like a virtual function
template <class T>
void Load(T*& t) {
t = static_cast<T*>(readNextObjectFromDataBase(typeid(T*))); // this
IS accepted, and perfectly OK, if no exception was thrown
}
A bad_cast exception is thrown from "readNextObjectFromDataBase()" if needed.
So, finally, since the implementations today act as if they send a 'const
type_info&' as a parameter to dynamic_cast, why not use it ?
Actually, I solved the problem by saving enough information, and the only
limitation is that you can't use the SAME data base between platforms, or
even compilers on the same platform.
Etay.
-- Etay Bogner,
-- Etay_Bogner@mail.stil.scitex.com,
-- Scitex Corp.
-- Israel.
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<9509081359.AA23162@ird.scitex.com>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backReferences:
<9509072134.AA20283@ird.scitex.com> <42nsuv$bqf@engnews2.Eng.Sun.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 08 Sep 95 21:35:38 GMT Approved: fjh@cs.mu.oz.au X-Original-Date: Fri, 8 Sep 1995 16:59:45 +0300 Message-ID: <9509081359.AA23162@ird.scitex.com> From: Etay_Bogner@mail.stil.scitex.com (Etay Bogner) Subject: Re: Add dynamic_cast<type_info&> Newsgroups: comp.std.c++ References: <9509072134.AA20283@ird.scitex.com> <42nsuv$bqf@engnews2.Eng.Sun.COM> Organization: Scitex Corp.