home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / gnu / g / help / 1218 < prev    next >
Encoding:
Text File  |  1992-09-11  |  2.7 KB  |  85 lines

  1. Newsgroups: bnr.lang.c++,gnu.g++.help
  2. Path: sparky!uunet!utcsri!torn!cunews!nrcnet0!bnrgate!bgtys9!bnr!crlstu4
  3. From: crlstu4@bnr.ca (Goetz von Escher)
  4. Subject: overloading delete operator in a base class
  5. Message-ID: <1992Sep11.150340.6759@bnr.ca>
  6. Keywords: gnu, c++, operator, delete
  7. Sender: crlstu4@bnr (Rob Borcic)
  8. Organization: Bell-Northern Research, Ottawa, Ontario, CANADA
  9. Date: Fri, 11 Sep 92 15:03:40 GMT
  10. Lines: 73
  11.  
  12. Hi folks!
  13.  
  14. I'm facing a simple problem. Maybe YOU can help:
  15.  
  16. ----------------------------------------------------------------
  17. MAIN PROBLEM:
  18. ----------------------------------------------------------------
  19. I want to keep track of the size of memory used by objects of
  20. specific classes (they are all derived from a common base
  21. class). So I override the new and delete operators of the
  22. base class and let a global integer count the number of
  23. bytes used.
  24.  
  25. To implement this I have to know the size of an object when it
  26. is deleted.
  27.  
  28. ----------------------------------------------------------------
  29. GNU PROBLEM:
  30. ----------------------------------------------------------------
  31. I want to overload the delete operator inside a class, using the
  32. two argument style (as expained in "The Annotated C++ reference
  33. manual" by Ellis/Stroustrup in section 12.5) as:
  34.  
  35.    void operator delete(void*, size_t);
  36.  
  37. Unfortunately g++ (version 2.2.2) does not accept this, as
  38. you can see when you try to compile the following code. If I
  39. remove the two occurrences of size_t the program works well.
  40.  
  41. Is this a gnu bug/feature or am I missing something here?
  42.  
  43. ----------------------------------------------------------------
  44. TRY TO COMPILE THIS PROGRAM:
  45. ----------------------------------------------------------------
  46. extern "C" {
  47. #include <stdlib.h>
  48. }
  49. #include <iostream.h>
  50.  
  51. class Simple {
  52.    public:
  53.       Simple()  { cout << "constructor\n"; };
  54.       ~Simple() { cout << "destructor\n"; };
  55.  
  56.       void operator delete (void*, size_t);
  57. };
  58.  
  59. void Simple::operator delete (void* o, size_t s) {
  60.    cout << "delete\n";
  61.    ::delete(o);
  62. }
  63.  
  64. int main() {
  65.    Simple* s = new Simple();
  66.    delete s;
  67.    return 0;
  68. }
  69. ----------------------------------------------------------------
  70. ERROR MESSAGE PRODUCED ON MY COMPUTER (SUN SPARC):
  71. ----------------------------------------------------------------
  72. [c++]% g++ overloading.delete.cxx
  73. overloading.delete.cxx: In method `Simple::~Simple ()':
  74. overloading.delete.cxx:9: too few arguments for method `operator delete'
  75.  
  76.  
  77. Thanks for any suggestions...
  78.  
  79. ----------------------------------------------------------------
  80. Goetz von Escher             PHONE (613) 763-8681
  81. 14 Viewmount Dr              EMAIL crlstu4@bnr.ca
  82. Nepean, Ontario K2G 1R5
  83. CANADA
  84. ----------------------------------------------------------------
  85.