Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!att!ulysses!andante!alice!shopiro From: shopiro@alice.UUCP (Jonathan Shopiro) Newsgroups: comp.lang.c++ Subject: Re: operator new() scoping Summary: members hide global functions with the same names Keywords: storage management garbage collection new delete private Message-ID: <9472@alice.UUCP> Date: 13 Jun 89 18:47:18 GMT References: <1989Jun7.115321.22620@mntgfx.mentor.com> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 60 In article <1989Jun7.115321.22620@mntgfx.mentor.com>, nmyers@mntgfx.mentor.com (Nathan Myers) writes: > 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. No. If the name is the same, the function is hidden. For example: void foo(int); struct S { void foo(); void bar(); }; void S::bar() { foo(1); // error: unexpected 1 argument for S::foo() } m o r e s i l l i n e s s -- Jonathan Shopiro AT&T Bell Laboratories, Warren, NJ 07060-0908 research!shopiro (201) 580-4229