| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
20 Dec 2007 10:00:35 PM |
| Object: |
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
.
|
|
| User: "jalina" |
|
| Title: Re: default parameters in constructor -- design issue |
21 Dec 2007 02:24:16 AM |
|
|
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
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
.
|
|
|
| User: "" |
|
| Title: Re: default parameters in constructor -- design issue |
21 Dec 2007 02:45:06 AM |
|
|
On Dec 21, 4:24=A0pm, jalina <jal...@nospam.please.com> wrote:
pauldepst...@att.net a =E9crit :
A colleague has written a counstructor of the form SomeClass(double a,
double b, double c) =A0 It then occurs to me that an indeterminate
number of other parameters are involved so I set up a struct
NewThingsToAdd =A0 =A0My intention is to create a constructor
SomeClass(double a, double b, double c, NewThingsToAdd d) =A0However, I
don't want to replace my colleague's constructor -- merely to allow an
enhancement. =A0The idea that occurs to me is to define a member m of
NewThingsToAdd such that m.prop1 =3D k1; m.prop2 =3D k2; =A0 etc (using
public access). =A0Then I could define the constructor SomeClass(double
a, double b, double c, NewThingsToAdd d =3D m) =A0
You could also make a second contructor with the extra parameter you need.=
Then my colleague's
code would still run using the particular member m. =A0That would work
quite nicely. =A0 =A0However, =A0the problem is that (as I understand it=
)
default parameters can't be members of classes so the above would lead
to illegal code. =A0Any 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.)
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
.
|
|
|
| User: "jalina" |
|
| Title: Re: default parameters in constructor -- design issue |
21 Dec 2007 03:21:18 AM |
|
|
a écrit :
On Dec 21, 4:24 pm, jalina <jal...@nospam.please.com> wrote:
pauldepst...@att.net 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
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.
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
.
|
|
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: default parameters in constructor -- design issue |
20 Dec 2007 10:09:25 PM |
|
|
wrote:
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
.
|
|
|
| User: "" |
|
| Title: Re: default parameters in constructor -- design issue |
20 Dec 2007 10:16:17 PM |
|
|
On Dec 21, 12:09=A0pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
pauldepst...@att.net wrote:
A colleague has written a counstructor of the form SomeClass(double a,
double b, double c) =A0 It then occurs to me that an indeterminate
number of other parameters are involved so I set up a struct
NewThingsToAdd =A0 =A0My intention is to create a constructor
SomeClass(double a, double b, double c, NewThingsToAdd d) =A0However, I
don't want to replace my colleague's constructor -- merely to allow an
enhancement. =A0The idea that occurs to me is to define a member m of
NewThingsToAdd such that m.prop1 =3D k1; m.prop2 =3D k2; =A0 etc (using
public access). =A0Then I could define the constructor SomeClass(double
a, double b, double c, NewThingsToAdd d =3D m) =A0 Then my colleague's
code would still run using the particular member m. =A0That would work
quite nicely. =A0 =A0However, =A0the problem is that (as I understand it=
)
default parameters can't be members of classes so the above would lead
to illegal code. =A0Any ideas how to implement the above.
The simplest way I've seen employs a static object of the same class
to initialise the argument. =A0Something like
=A0 =A0 class NewThingsToAdd {
=A0 =A0 =A0 =A0 ...
=A0 =A0 =A0 =A0 static NewThingsToAdd defarg;
=A0 =A0 };
=A0 =A0 class SomeClass {
=A0 =A0 =A0 =A0 ...
=A0 =A0 =A0 =A0 SomeClass(double, double, double,
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 NewThingsToAdd const& =3D NewThing=
sToAdd::defarg);
=A0 =A0 };
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
.
|
|
|
|
|

|
Related Articles |
|
|