Path: utzoo!attcan!uunet!zephyr.ens.tek.com!uw-beaver!ssc-vax!uvicctr.UVic.CA!mjunkin From: mjunkin@uvicctr.UVic.CA.UUCP (Michael Junkin) Newsgroups: comp.lang.c++ Subject: C++ and operator new() Message-ID: <1155@uvicctr.UVic.CA.UUCP> Date: 10 Jul 90 16:44:58 GMT Reply-To: mjunkin@uvicctr.UUCP (Michael Junkin) Organization: University of Victoria, Victoria B.C. Canada Lines: 71 Hello: I'm having a bit of trouble with operator new. Specifically, I have a class X for which I often (but not always) want to allocate storage myself but sometimes want to use the builtin operator new. Thus, the class X contains the definition static void *operator new(size_t); The intent is that X *x = new X(...); (1) can be used to allocate and construct local objects while X *x = ::new X(...); (2) is used to allocate and construct global objects of class X. However, this does not work. The C code generated by both the AT&T and SUN version 2.0 compilers is the same in both case 1 and case 2. As an example the program // ********************************* #include #include class X { int i; public: X(int n) { i = n; } static void *operator new(size_t); static void operator delete(void *) {} }; void *X::operator new(size_t n) { cout << "allocating locally\n"; return new char[n]; } main() { X *x1 = ::new X(1); X *x2 = new X(2); } // ********************************* when compiled and run produces the output allocating locally allocating locally Have I missed something, or is this a C++ compiler bug? I would appreciate any information on this matter. Thanks in advance, Michael. -- ------------------------------------------------------------------------ *standard disclaimers apply* Michael D. Junkin Department of Computer Science University of Victoria Victoria, B.C. CANADA mjunkin@csr.UVic.ca (internet)