Topic: Safe deletes with type_info data?
Author: Benjamin Scherrey <scherrey@switchco.com>
Date: 2000/08/27 Raw View
If I have the following:
struct obj
{
void* ptr;
const type_info* otype;
};
Where obj.ptr is a pointer to a dynamically allocated instance
of a class
whose type_info data is pointed to by obj.otype:
1. Is it possible to safely delete the object pointed to by
obj.ptr and
have it destruct cleanly?
2. What if obj.otype points to the type_info data of a base
class (with a
virtual dtor defined) of the object's class pointed to by
obj.ptr?
thanx & later,
Ben Scherrey
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/08/28 Raw View
Benjamin Scherrey wrote:
>
> If I have the following:
>
> struct obj
> {
> void* ptr;
> const type_info* otype;
> };
>
> Where obj.ptr is a pointer to a dynamically allocated instance
> of a class
> whose type_info data is pointed to by obj.otype:
>
> 1. Is it possible to safely delete the object pointed to by
> obj.ptr and
> have it destruct cleanly?
Yes, but not with that information alone. The code that deletes the
object must know at least a base class type, and not merely the address
of a type_info object for that type. The std::type_info class has
significantly less functionality than it could have.
> 2. What if obj.otype points to the type_info data of a base
> class (with a
> virtual dtor defined) of the object's class pointed to by
> obj.ptr?
Same problem.
What you can do is declare a base class type with a virtual destructor,
and derive all of the types you'd want to store pointers to from that
type. Then you wouldn't need your 'obj' class to wrap 'ptr' in.
However, C++ doesn't provide a general object class that all other types
are derived from. Given typical implementations, if it did have such a
general object class, you wouldn't want it to have the virtual
destructor this technique requires; carry around a vtable pointer with
every 'int' would be rather expensive.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: youngmm@wku.edu (Mark M. Young)
Date: 2000/08/29 Raw View
I might have a use for something similar to this. Does C++ have
anything with allows the user to cast something base on a 'type_info'?
bar = reinterpret_cast<typeinfo( item.type_ )>( item.data_ );
Also, how come 'typeof()' is not in the standard?
Mark M. Young
roughly
typedef struct my_rtti
{
void *data_;
const type_info *type_;
} my_rtti_t;
--
Any hyperlinks appearing in this article were inserted by the unscrupulous
operators of a Usenet-to-web gateway, without obtaining the proper permission
of the author, who does not endorse any of the linked-to products or services.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/08/29 Raw View
"Mark M. Young" wrote:
>
> I might have a use for something similar to this. Does C++ have
> anything with allows the user to cast something base on a 'type_info'?
No.
> bar = reinterpret_cast<typeinfo( item.type_ )>( item.data_ );
>
> Also, how come 'typeof()' is not in the standard?
>
> Mark M. Young
>
> roughly
> typedef struct my_rtti
> {
> void *data_;
> const type_info *type_;
> } my_rtti_t;
Among other useful things that type_info could provide, but doesn't:
1. traits information about a type, such as isClass, isPointer, etc.
2. a function returning a void* pointer to a newly default-constructed
instance of the type. Returns (void*)NULL if for any reason such an
instance cannot be constructed.
3. A count of the number of direct base classes. This will be 0 if
!isClass.
4. A function taking an integer argument 'n' returning a type_info
pointer to the (n+1)th base class. Returns (type_info*)NULL if 'n' is
invalid. There should also be some way of determining whether the
derivation is public, private, or protected, and whether or not it is
virtual.
5. A function for testing whether the type represented by one type_info
is derived, directly or indirectly. from the type represented by another
type_info.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]