🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: bnr.lang.c++
Article #1

overloading delete operator in a base class

#1
From: Goetz von Escher
Date: Fri, 11 Sep 1992 15:03
73 lines
2824 bytes
Hi folks!

I'm facing a simple problem. Maybe YOU can help:

----------------------------------------------------------------
MAIN PROBLEM:
----------------------------------------------------------------
I want to keep track of the size of memory used by objects of
specific classes (they are all derived from a common base
class). So I override the new and delete operators of the
base class and let a global integer count the number of
bytes used.

To implement this I have to know the size of an object when it
is deleted.

----------------------------------------------------------------
GNU PROBLEM:
----------------------------------------------------------------
I want to overload the delete operator inside a class, using the
two argument style (as expained in "The Annotated C++ reference
manual" by Ellis/Stroustrup in section 12.5) as:

   void operator delete(void*, size_t);

Unfortunately g++ (version 2.2.2) does not accept this, as
you can see when you try to compile the following code. If I
remove the two occurrences of size_t the program works well.

Is this a gnu bug/feature or am I missing something here?

----------------------------------------------------------------
TRY TO COMPILE THIS PROGRAM:
----------------------------------------------------------------
extern "C" {
#include <stdlib.h>
}
#include <iostream.h>

class Simple {
   public:
      Simple()  { cout << "constructor\n"; };
      ~Simple() { cout << "destructor\n"; };

      void operator delete (void*, size_t);
};

void Simple::operator delete (void* o, size_t s) {
   cout << "delete\n";
   ::delete(o);
}

int main() {
   Simple* s = new Simple();
   delete s;
   return 0;
}
----------------------------------------------------------------
ERROR MESSAGE PRODUCED ON MY COMPUTER (SUN SPARC):
----------------------------------------------------------------
[c++]% g++ overloading.delete.cxx
overloading.delete.cxx: In method `Simple::~Simple ()':
overloading.delete.cxx:9: too few arguments for method `operator delete'


Thanks for any suggestions...

----------------------------------------------------------------
Goetz von Escher             PHONE (613) 763-8681
14 Viewmount Dr              EMAIL crlstu4@bnr.ca
Nepean, Ontario K2G 1R5
CANADA
----------------------------------------------------------------
Technical Details
Message-ID: <1992Sep11.150340.6759@bnr.ca>
Path: arc1-ca.pugleaf.net!sparky!uunet!utcsri!torn!cunews!nrcnet0!bnrgate!bgtys9!bnr!crlstu4

Show Full Headers
Complete Article Headers
Newsgroups: bnr.lang.c++,gnu.g++.help
Path: arc1-ca.pugleaf.net!sparky!uunet!utcsri!torn!cunews!nrcnet0!bnrgate!bgtys9!bnr!crlstu4
From: crlstu4@bnr.ca (Goetz von Escher)
Subject: overloading delete operator in a base class
Message-ID: <1992Sep11.150340.6759@bnr.ca>
Keywords: gnu, c++, operator, delete
Sender: crlstu4@bnr (Rob Borcic)
Organization: Bell-Northern Research, Ottawa, Ontario, CANADA
Date: Fri, 11 Sep 92 15:03:40 GMT
Lines: 73