home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!Germany.EU.net!unido!news.Hamburg.Germany.EU.net!easix!ibg1!ado
- From: ado@ibg1.ibg.sub.org (Christoph Adomeit)
- Newsgroups: comp.lang.c++
- Subject: Determining object type at runtime ?
- Message-ID: <Bs43r6.C24@ibg1.ibg.sub.org>
- Date: 28 Jul 92 18:37:54 GMT
- Organization: ibg
- Lines: 40
-
- Hi C++ freaks,
-
- I need an idea about how to solve the following problem:
- I embedded some C++ classes into an interpreter (perl).
- Perl only understands C-calls, so I wrapped some functions
- like
-
- extern "C"
- {
- void *new_myobject(constructor vars)
- { return (void*)new myobject(constructor vars); }
-
- int myobject_print(void *myobj)
- {
- myobject *object=(myobject*)myobj;
- return object->print;
- }
- }
- ...
-
- I can get a reference to myobject from perl by calling &new_myobject.
- Afterwards I can work from perl with the class like this:
- $object=&new_myobject(constructor vars);
- $result=&myobject_print($object);
-
- everything works fine !. The only Problem is, if the ptr passed to
- myobject print is not a pointer to a class "myobject". The program
- will of course dump core. How I can make sure that myobject->print
- is only called with a valid ptr to an object of type myobject ?
-
- One solution would be to implement a method "type" to all known objects
- and compare object->type, but most of the time i call functions with
- int's char's ... for which I can't implement such a method.
-
- Another idea would be to keep a list of valid pointers for each known
- object and look up the adress. I think it's the only way.
-
- Any idea ?
- --
- Origin: It's not a bug, it's a feature !
-