home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11675 < prev    next >
Encoding:
Text File  |  1992-07-28  |  1.5 KB  |  50 lines

  1. Path: sparky!uunet!mcsun!Germany.EU.net!unido!news.Hamburg.Germany.EU.net!easix!ibg1!ado
  2. From: ado@ibg1.ibg.sub.org (Christoph Adomeit)
  3. Newsgroups: comp.lang.c++
  4. Subject: Determining object type at runtime ?
  5. Message-ID: <Bs43r6.C24@ibg1.ibg.sub.org>
  6. Date: 28 Jul 92 18:37:54 GMT
  7. Organization: ibg
  8. Lines: 40
  9.  
  10. Hi C++ freaks,
  11.  
  12. I need an idea about how to solve the following problem:
  13. I embedded some C++ classes into an interpreter (perl).
  14. Perl only understands C-calls, so I wrapped some functions
  15. like
  16.  
  17. extern "C"
  18. {
  19.   void *new_myobject(constructor vars)
  20.   { return (void*)new myobject(constructor vars); }
  21.  
  22.     int myobject_print(void *myobj)
  23.     { 
  24.         myobject *object=(myobject*)myobj;
  25.         return object->print;
  26.     }
  27. }
  28.     ...
  29.  
  30. I can get a reference to myobject from perl by calling &new_myobject.
  31. Afterwards I can work from perl with the class like this:
  32. $object=&new_myobject(constructor vars);
  33. $result=&myobject_print($object);
  34.  
  35. everything works fine !. The only Problem is, if the ptr passed to
  36. myobject print is not a pointer to a class "myobject". The program
  37. will of course dump core. How I can make sure that myobject->print
  38. is only called with a valid ptr to an object of type myobject ?
  39.  
  40. One solution would be to implement a method "type" to all known objects
  41. and compare object->type, but most of the time i call functions with
  42. int's char's ... for which I can't implement such a method.
  43.  
  44. Another idea would be to keep a list of valid pointers for each known
  45. object and look up the adress. I think it's the only way.
  46.  
  47. Any idea ?
  48. -- 
  49. Origin: It's not a bug, it's a feature !
  50.