On Dec 28 2007, 2:17 am, lord trousers <neil.toro...@gmail.comwrote:
I'm implementing a garbage collector in C++ for a fun new
language (don't ask), and I've found that it spends a good
amount of time calling "finalize" on objects that are
freed/not relocated. The default implementation does nothing
and always will, and most types won't need to override it.
It'd be nice if I could test for overrides, but the obvious
thing doesn't work:
class Object {
// ...
virtual void finalize() { }
// ...
};
// In the collector:
Object* pobj = <stuff>;
if (&pobj->finalize != &hv_Object::finalize)
pobj->finalize();
// GCC:
// ISO C++ forbids taking the address of a bound member function
// to form a pointer to member function. Say
'&hv_Object::finalize'
I'd love to do what it says, but I don't know the type of the
object. Is there an easy way to make this same test? I want
to avoid RTTI and compiler-specific language extensions, and
also make it externally transparent.
Let me see if I understand: you want to avoid RTTI, but you want
RTTI. As soon as you try to find out something about the
dynamic type, you need RTTI---generally, a virtual function is
cheaper (in runtime) than anything else.
With regards to finalization with garbage collection, the usual
solution is to require explicit registration. This means a
(very slight) amount of extra work for the programmer who needs
finalization (but such cases are rare), but also means that you
don't have to derive everything from a common base---garbage
collection can also work for arrays of char, or what have you.
(This is how the Boehm collector works, but of course, it also
works with C, where virtual functions aren't an option.)
--
James Kanze (GABI Software) mailto:ja*********@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34