473,473 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Dec 21 '07 #1
5 1416
pa**********@att.net 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
Dec 21 '07 #2
On Dec 21, 12:09*pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
pauldepst...@att.net 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

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
Dec 21 '07 #3
pa**********@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
Dec 21 '07 #4
On Dec 21, 4:24*pm, jalina <jal...@nospam.please.comwrote:
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.)

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
Dec 21 '07 #5
pa**********@att.net a écrit :
On Dec 21, 4:24 pm, jalina <jal...@nospam.please.comwrote:
>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
Dec 21 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
10
by: Ook | last post by:
I'm having trouble comprehending what exactly "default construction" is. I know how to provide a constructor with initial values, so that if I, for example, in my code do this: MyClass...
8
by: Sam Kuehn | last post by:
How do I accomplish the fallowing (is it even possible). Say I write a UserControl "MyControl.ascx". Now I use LoadControl("MyControl.ascx"). But I really want MyControl to require parameters in...
12
by: Dave A | last post by:
I have a class that does not have a default constructor. The nature of the class inherently excludes it from having one and to put one in will blatantly misrepresent the object that it is modelling....
3
by: denis_browne | last post by:
When writing the following code: class Base { Base(const Base &rhs) {} ~Base(); }; void f() { Base b;
12
by: Edward Diener | last post by:
Given value class X { public: // Not allowed: X():i(100000),s(10000) { } // Allowed void InitializeDefaults() { i = 100000; s = 10000; } private: int i;
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.