Path: utzoo!utgpu!utstat!jarvis.csri.toronto.edu!rutgers!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!zephyr!tektronix!sequent!mntgfx!nmyers From: nmyers@mntgfx.mentor.com (Nathan Myers) Newsgroups: comp.lang.c++ Subject: operator new() scoping Keywords: storage management garbage collection new delete private Message-ID: <1989Jun7.115321.22620@mntgfx.mentor.com> Date: 7 Jun 89 18:53:20 GMT Organization: Mentor Graphics Corporation, Beaverton Oregon Lines: 35 Providing arguments for operators new() and delete() is a major advance. It offers more power than "this=" semantics, and allows smaller, faster code. However, ark@alice.UUCP (Andrew Koenig) writes > Operator new (and operator delete) obeys the same scope rules as any > other member function: if defined inside a class, operator new hides > any global operator new. For example: > > class T { > /* stuff */ > public: > void* operator new(size_t, Memory_speed); > /* more stuff */ > }; > > T* tp = new T; // Error! > > The use of `new T' is incorrect in this example because the member > operator new hides the global operator new, so no operator new > can be found for T that does not require a second argument. This seems inconsistent to me. Other overloaded functions don't hide their neighbors, why should operator new()? The standard way to hide a global is to declare a member with the same name and arguments. Then, if I (as class T author) want to disallow ::operator new(size_t), I can declare a private one and not define it. A self-consistent alternative to the posted semantics is: 1. Member operator new(size_t, class Foo) doesn't hide global ::new(size_t). 2. Member operator new(size_t) may be declared private. 3. "::new T" is not valid syntax. (It seems unnecessary, and dangerous.) Nathan Myers nmyers@pdx.mentor.com