Path: utzoo!news-server.csri.toronto.edu!rutgers!uwm.edu!zaphod.mps.ohio-state.edu!caen!ox.com!yale!cs.utexas.edu!radar!cadillac!pebbles!ned From: ned@pebbles.cad.mcc.com (Ned Nowotny) Newsgroups: comp.lang.c++ Subject: Re: Sun C++ 2.0 - Default Argument Question Message-ID: <17237@cadillac.CAD.MCC.COM> Date: 9 Mar 91 18:33:13 GMT References: Sender: news@cadillac.CAD.MCC.COM Reply-To: ned%cad@MCC.COM (Ned Nowotny) Distribution: na Organization: MCC CAD Program, Austin, TX Lines: 89 In article dsouza@optima.cad.mcc.com (Desmond Dsouza) writes: => =>In article leo@duttnph.tudelft.nl (Leo Breebaart) writes: => => > The thing is: we are trying to port a project from GNU g++ to Sun C++ => > v. 2.0, and the following type of constructor => > => > SomeClass(String = "woppa"); => > => > generates a "sorry, not implemented: constructor needed for argument => > initializer" error. (String is my own implementation of a String class.) => > => > My main problem is that for the life of me I cannot see a possible => > workaround, and of course our code uses dozens of these constructors => > (which GNU never had any trouble with). => > => > So: => > => > 1) Is using such a default argument a form of bad programming, and if so, => > why? => > => > 2) If not, what can I possibly do to get my code running under Sun C++? => =>Make String ("woppa") a static, private member inside SomeClass, and =>use that as a default in the constructor. => => > Leo Breebaart (leo @ duttnph.tudelft.nl) => =>Desmond. Actually, it is easer to just provide a second constructor: class SomeClass { . . . SomeClass(String); SomeClass(); . . . }; . . . SomeClass( String string ) { . . . } SomeClass() { String string( "woppa" ); . . . } In fact, if the argument is just used to initialize a member variable, the definitions become: SomeClass( String string ) : MemberString( string ) { . . . } SomeClass() : MemberString( "woppa" ) { . . . } In general, overloaded functions are more flexible than default arguments and, with judicious use of inline'ing, no less efficient. Except for their extensive use in existing practice, I would argue for the abolition of default arguments altogether. Ned Nowotny, MCC CAD Program, Box 200195, Austin, TX 78720 Ph: (512) 338-3715 ARPA: ned@mcc.com UUCP: ...!cs.utexas.edu!milano!cadillac!ned ------------------------------------------------------------------------------- "We have ways to make you scream." - Intel advertisement in the June 1989 DDJ.