default parameters in constructor -- design issue

P

pauldepstein

A colleague has written a counstructor of the form SomeClass(double a,
double b, double c) It then occurs to me that an indeterminate
number of other parameters are involved so I set up a struct
NewThingsToAdd My intention is to create a constructor
SomeClass(double a, double b, double c, NewThingsToAdd d) However, I
don't want to replace my colleague's constructor -- merely to allow an
enhancement. The idea that occurs to me is to define a member m of
NewThingsToAdd such that m.prop1 = k1; m.prop2 = k2; etc (using
public access). Then I could define the constructor SomeClass(double
a, double b, double c, NewThingsToAdd d = m) Then my colleague's
code would still run using the particular member m. That would work
quite nicely. However, the problem is that (as I understand it)
default parameters can't be members of classes so the above would lead
to illegal code. Any ideas how to implement the above.

Many Thanks,

Paul Epstein
 
V

Victor Bazarov

A colleague has written a counstructor of the form SomeClass(double a,
double b, double c) It then occurs to me that an indeterminate
number of other parameters are involved so I set up a struct
NewThingsToAdd My intention is to create a constructor
SomeClass(double a, double b, double c, NewThingsToAdd d) However, I
don't want to replace my colleague's constructor -- merely to allow an
enhancement. The idea that occurs to me is to define a member m of
NewThingsToAdd such that m.prop1 = k1; m.prop2 = k2; etc (using
public access). Then I could define the constructor SomeClass(double
a, double b, double c, NewThingsToAdd d = m) Then my colleague's
code would still run using the particular member m. That would work
quite nicely. However, the problem is that (as I understand it)
default parameters can't be members of classes so the above would lead
to illegal code. Any ideas how to implement the above.

The simplest way I've seen employs a static object of the same class
to initialise the argument. Something like

class NewThingsToAdd {
...
static NewThingsToAdd defarg;
};

class SomeClass {
...
SomeClass(double, double, double,
NewThingsToAdd const& = NewThingsToAdd::defarg);
};

Don't forget to define the static member of 'NewThingsToAdd'.

V
 
P

pauldepstein

The simplest way I've seen employs a static object of the same class
to initialise the argument.  Something like

    class NewThingsToAdd {
        ...
        static NewThingsToAdd defarg;
    };

    class SomeClass {
        ...
        SomeClass(double, double, double,
                    NewThingsToAdd const& = NewThingsToAdd::defarg);
    };

Don't forget to define the static member of 'NewThingsToAdd'.

V


Thanks, Victor. Actually, I didn't know this is legal. As a newbie,
the only default params I've seen are of types like int or double etc.
not user-defined class types.

Paul Epstein
 
J

jalina

(e-mail address removed) a écrit :
A colleague has written a counstructor of the form SomeClass(double a,
double b, double c) It then occurs to me that an indeterminate
number of other parameters are involved so I set up a struct
NewThingsToAdd My intention is to create a constructor
SomeClass(double a, double b, double c, NewThingsToAdd d) However, I
don't want to replace my colleague's constructor -- merely to allow an
enhancement. The idea that occurs to me is to define a member m of
NewThingsToAdd such that m.prop1 = k1; m.prop2 = k2; etc (using
public access). Then I could define the constructor SomeClass(double
a, double b, double c, NewThingsToAdd d = m)

You could also make a second contructor with the extra parameter you need.

Then my colleague's
 
P

pauldepstein

(e-mail address removed) a écrit :


You could also make a second contructor with the extra parameter you need.

Then my colleague's






- Show quoted text -

I thought of that. The prob with that is that it would involve a huge
amount of copy-pasting as much of my colleague's constructor applies
to my own. (Not that you could have known that, of course.)

This raises an interesting question of whether that is bad. If so
why? Suppose a body of code contains several blocks of 200 lines or
so which are identical. Is that something to avoid? If so why? I
think this result is known as code-bloat.

Paul Epstein
 
J

jalina

(e-mail address removed) a écrit :
I thought of that. The prob with that is that it would involve a huge
amount of copy-pasting as much of my colleague's constructor applies
to my own. (Not that you could have known that, of course.)

Put the common code in an init(...) function. I think the FAQ has an
entry for that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top