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

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.

Replies are listed 'Best First'.
Constructor parameter management
by boo_radley (Parson) on Dec 15, 2001 at 04:44 UTC
    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