Connecting Tech Pros Worldwide Forums | Help | Site Map

default parameters in constructor -- design issue

pauldepstein@att.net
Guest
 
Posts: n/a
#1: Dec 21 '07
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

Victor Bazarov
Guest
 
Posts: n/a
#2: Dec 21 '07

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


pauldepstein@att.net
Guest
 
Posts: n/a
#3: Dec 21 '07

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
jalina
Guest
 
Posts: n/a
#4: Dec 21 '07

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
pauldepstein@att.net
Guest
 
Posts: n/a
#5: Dec 21 '07

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:
Many Thanks,
>
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
jalina
Guest
 
Posts: n/a
#6: Dec 21 '07

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
Closed Thread