Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!ames!pasteur!helios.ee.lbl.gov!nosc!cod!mball From: mball@cod.NOSC.MIL (Michael S. Ball) Newsgroups: comp.lang.c++ Subject: Re: Overloading operator new() Message-ID: <1239@cod.NOSC.MIL> Date: 5 Oct 88 01:56:18 GMT References: <5949@june.cs.washington.edu> Reply-To: mball@cod.nosc.mil.UUCP (Michael S. Ball) Organization: Naval Ocean Systems Center, San Diego Lines: 37 In article <5949@june.cs.washington.edu> david@uw-june.UUCP (David Callahan) writes: >Stroustrup lists the "new" operator as one that can be >overloaded. I'm not exactly sure what functionality this >provides (over assigning into "this" in the constructor) >and I know I can't get the syntax right. Could someone >show me an example of "new" being overloaded and how >it would be used? As far as I know, the feature is documented only in the "Evolution of C++" paper in the Santa Fe workshop proceedings. The syntax is simple: class foo { : : void * operator new(long); : }; Your version of cfront may not accept it, however, as it is a recent addition to the language. It adds no new functionality over assignment to this, but is less error-prone and puts all of the allocation code in one place, rather than replicated in each constructor. Assignment to "this" has a number of traps into which one may fall. For example, you must make sure that there is an assignment to this on all possible paths through the constructor. Otherwise, member constructors may not be called. Overloading "operator new" seems much cleaner. A side effect is that the resulting code will probably be better since the member and base class initializations do not have to be replicated as they do when "this" is assigned to. Mike Ball TauMetric Corporation 1094 Cudahy Pl. Ste 302 San Diego, CA 92110 (619)275-6381