default parameters in constructor -- design issue 
December 21st, 2007, 03:05 AM
| | | default parameters in constructor -- design issue
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 | 
December 21st, 2007, 03:15 AM
| | | Re: default parameters in constructor -- design issue pauldepstein@att.net wrote: Quote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask | 
December 21st, 2007, 03:25 AM
| | | Re: default parameters in constructor -- design issue
On Dec 21, 12:09*pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote: Quote:
pauldepst...@att.net wrote: Quote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
|
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 | 
December 21st, 2007, 07:25 AM
| | | Re: default parameters in constructor -- design issue pauldepstein@att.net a écrit : Quote:
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 Quote:
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
| | 
December 21st, 2007, 07:55 AM
| | | Re: default parameters in constructor -- design issue
On Dec 21, 4:24*pm, jalina <jal...@nospam.please.comwrote: Quote:
pauldepst...@att.net a écrit :
> Quote:
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
>
>
> Quote:
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.
| >> Quote: |
Paul Epstein- Hide quoted text -
| >
- 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 | 
December 21st, 2007, 08:25 AM
| | | Re: default parameters in constructor -- design issue pauldepstein@att.net a écrit : Quote:
On Dec 21, 4:24 pm, jalina <jal...@nospam.please.comwrote: Quote:
>pauldepst...@att.net a écrit :
>> Quote:
>>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
>>
>>
>> Quote:
>>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- Hide quoted text -
| >- 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.)
| Put the common code in an init(...) function. I think the FAQ has an
entry for that. Quote:
>
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
| | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|