[comp.lang.c++] Sun C++ 2.0 - Default Argument Question

dsouza@optima.cad.mcc.com (Desmond Dsouza) (02/26/91)

In article <leo.667062578@galaxy> 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.
--

-------------------------------------------------------------------------------
 Desmond D'Souza, MCC CAD Program | ARPA: dsouza@mcc.com | Phone: [512] 338-3324
 Box 200195, Austin, TX 78720 | UUCP: {uunet,harvard,gatech,pyramid}!cs.utexas.edu!milano!cadillac!dsouza

ned@pebbles.cad.mcc.com (Ned Nowotny) (03/10/91)

In article <DSOUZA.91Feb25171047@optima.cad.mcc.com> dsouza@optima.cad.mcc.com (Desmond Dsouza) writes:
=>
=>In article <leo.667062578@galaxy> 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.