home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / std / cplus / 1123 < prev    next >
Encoding:
Text File  |  1992-09-02  |  2.2 KB  |  66 lines

  1. Path: sparky!uunet!olivea!hal.com!hal!brennan
  2. From: brennan@hal.com (Dave Brennan)
  3. Newsgroups: comp.std.c++
  4. Subject: Which delete operator should be called?
  5. Message-ID: <BRENNAN.92Sep3010854@yosemite.hal.com>
  6. Date: 3 Sep 92 07:08:54 GMT
  7. Organization: HaL Computer Systems, Austin, TX
  8. Lines: 55
  9. NNTP-Posting-Host: yosemite.hal.com
  10.  
  11. It's not clear to me which delete operator should be called in the
  12. following example program.  The ARM says that the delete operator is
  13. inherited, but is not virtual (which certainly makes since, because it
  14. must be static).
  15.  
  16. All compilers that I testest this on indicated that the derived delete
  17. operator is called when the destructor is virtual.  If the destructor is
  18. non-virtual the base class delete operator is called.  So at least in some
  19. compilers the delete operator IS virtual if the destructor is.  This may be
  20. reasonable behavior but seems contrary to what the ARM says.
  21.  
  22. Somewhat related to this, I need to be able to call the virtual destructor
  23. of an object without deleting it.  I can't get any compilers to accept any
  24. destructor calling syntax other than "class::~class()" (where "class" is a
  25. class name).  In cfront this syntax DOES call the virtual destructor (ie:
  26. if I call "base::~base()" from a base method, the derived destructor is
  27. called).  Lucid CC it only called the base class destructor, which seems
  28. like the correct behavior.  g++ 2.2.2 wouldn't accept any kind of explicit
  29. calls to a destructor.  Can anyone tell me how to call a virtual destructor
  30. in a way that least cfront 2.1 will accept and is correct C++?
  31.  
  32. extern "C" void puts (char *);
  33.  
  34. class base
  35. {
  36. public:
  37.   virtual ~base ()
  38.     { puts ("destruct base"); }
  39.   void operator delete (void *)
  40.     { puts ("delete base"); }
  41.   void destruct ()
  42.     { base::~base (); }
  43. };
  44.  
  45. class derived : public base
  46. {
  47. public:
  48.   ~derived ()
  49.     { puts ("destruct derived"); }
  50.   void operator delete (void *)
  51.     { puts ("delete derived"); }
  52. };
  53.  
  54. int
  55. main ()
  56. {
  57.   base *b = new derived;
  58.  
  59.   delete b;
  60. }
  61. --
  62. Dave Brennan                                      HaL Computer Systems
  63. brennan@hal.com                                         (512) 794-2855
  64.  
  65. Visit the Emacs Lisp Archive: archive.cis.ohio-state.edu:pub/gnu/emacs
  66.