Article View: gnu.g++
Article #312Overloading new and delete in g++
From: "Douglas C. Schmidt"
Date: Thu, 06 Oct 1988 22:20
Date: Thu, 06 Oct 1988 22:20
61 lines
1618 bytes
1618 bytes
Hi,
I recently discovered the joy of overloading keyword new in G++.
However, is it possible to overload delete as well? Here's the example:
----------------------------------------
#include <stream.h>
const int Max_Buf = 10;
class foo {
private:
int j;
int k;
static int *Head, *Ptr;
public:
foo(int i) {
j = k = i;
}
int Return(void) {
return(j * k);
}
void * operator new(long Size) {
if (!Head) {
Ptr = Head = (int *) malloc(Size * Max_Buf);
}
void *Temp = Ptr--;
if (Ptr <= Head) {
Head = 0;
}
cout << "Temp = " << int(Temp) << "\n";
return(Temp);
}
void operator delete(long size) {
cout << "Hello\n";
}
};
main() {
foo Bar ( 1 );
cout << "Bar.Return() = " << Bar.Return() << "\n";
for ( int i = 0; i < 20; i++ ) {
foo *Bard = new foo(i);
cout << "&Bard = " << int(Bard) << "\n";
delete Bard;
}
}
----------------------------------------
This doesn't appear to call delete in version 1.27. Am I using
the correct syntax and semantics?
thanks,
Doug
Technical Details
Message-ID:
<8810061520.aa13248@PARIS.ICS.UCI.EDU>Path:
arc1-ca.pugleaf.net!utzoo!telly!ddsw1!lll-winken!killer!osu-cis!tut.cis.ohio-state.edu!ORION.CF.UCI.EDU!schmidt%crimee.ics.uci.eduShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!telly!ddsw1!lll-winken!killer!osu-cis!tut.cis.ohio-state.edu!ORION.CF.UCI.EDU!schmidt%crimee.ics.uci.edu
From: schmidt%crimee.ics.uci.edu@ORION.CF.UCI.EDU ("Douglas C. Schmidt")
Newsgroups: gnu.g++
Subject: Overloading new and delete in g++
Message-ID: <8810061520.aa13248@PARIS.ICS.UCI.EDU>
Date: 6 Oct 88 22:20:14 GMT
Sender: daemon@tut.cis.ohio-state.edu
Distribution: gnu
Organization: GNUs Not Usenet
Lines: 60