in reply to OOP, ".NET", Perl, and all that

Default initiailizer values are a standard in the C++ language. (I'd quote the C++ reference, but I don't have it handy). I remember using this to also have functions with 'optional' arguments that would be filled in by the default initializers.

Now, while Perl doesn't have this directly, it's very easy to do , particullary if you pass arguments by hash. Damian's OOP has examples like:

my %func_defaults = ( a=>3, b=>4, c=>5 ); sub func { %my_args = ( %func_defaults, @_ ); ... }
Or of course, you have friendly operators like ||= (or //= that is in Perl6). It's not as 'simple' as C++ (or C#, apparently)'s default arguments, but they're still there.

I know Java doesn't have this feature, nor C.

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important

Replies are listed 'Best First'.
Re: Re: OOP,
by John M. Dlugosz (Monsignor) on Dec 15, 2001 at 01:35 UTC
    You're thinking of default parameter values in C++. That's only good for the single function (e.g. constructor) in which it appears.

    I'm talking about not having to specify the (same) initial value for instance data among several constructors.

      Are you familiar with "fallthrough" constructors? you only specify a given parameter at the appropriate "level" of constructor .
      given our object with say, x and y and z:
      object(xx,yy,zz) { x=xx; y=yy; z=zz } object (xx,yy) { object(xx,yy,defaultz) } object (xx) { object(xx,defaulty) } object (){ object(defaultx) }

      close?

        I'm familiar with the concept of using common code for multiple constructors. It's not really the same thing, but it's used for the same purpose; in fact, it's probably more general. It's difficult to apply to C++ because initialization semantics and syntax are not conducive. However, using an intermediate base class does the trick here.

        To do the same thing as these declared initial values, you need to derive a new type for each member in question, to arrange it so its default value is exactly what you wanted.

        —John