Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
c++ - overloaded new[] and delete[] are not called
Overloading new and delete for a class works fine, but a redefinition of operator new[] or delete[] is ignored. Here is a small test case: #include <iostream.h> #include <stdlib.h> class test { int a; public: test() {cout << "construct\n";} ~test() {cout << "destruct\n";} void* operator new(size_t sz) {cout << "new\n"; return malloc(sz);} void operator delete(void* p) {cout << "delete\n"; free(p);} void* operator new[](size_t sz) {cout << "new[]\n"; return malloc(sz);} void operator delete[](void* p) {cout << "delete[]\n"; free(p);} }; int main() { test *t= new test; delete t; t= new test[4]; delete[] t; } Compiled with version 8.38 the output is: new construct destruct delete construct construct construct construct destruct destruct destruct destruct The overloaded new and delete were called as expected, but ::new[] and ::delete[] were called instead of test::new[] and test::delete[]. Why? Jan 20 2004
In article <bul3mu$2bf3$1 digitaldaemon.com>, Steve Strand says...Overloading new and delete for a class works fine, but a redefinition of operator new[] or delete[] is ignored. Jan 21 2004
dan wrote:In article <bul3mu$2bf3$1 digitaldaemon.com>, Steve Strand says...Overloading new and delete for a class works fine, but a redefinition of operator new[] or delete[] is ignored. Jan 21 2004
Thanks for pointing out the -Aa flag, but unfortunately I get no difference when I compile with -Aa or -A. Can anyone else compile my short example and have the overloaded new[] and delete[] called? P.S. for Walter: perhaps give an error message when -Aa is needed (like already happens if you forget -Ae and use exceptions) Jan 21 2004
In article <bumimm$1kaf$1 digitaldaemon.com>, Steve Strand says..... Can anyone else compile my short example and have the overloaded new[] and delete[] called? Jan 21 2004
|