Connecting Tech Pros Worldwide Forums | Help | Site Map

Re: Trick needed - passing a large number of parameters to a class

acehreli@gmail.com
Guest
 
Posts: n/a
#1: Jul 21 '08
On Jul 21, 1:27*pm, eran <kale...@gmail.comwrote:
Quote:
- Use a params struct
... that has a constructor to set all the members.
Quote:
What bothers me is this: I'd really like to minimize the chance that
the caller will miss setting one of the params.
That problem is solved... :)

Ali
khalid302@gmail.com
Guest
 
Posts: n/a
#2: Jul 21 '08

re: Re: Trick needed - passing a large number of parameters to a class


On Jul 21, 11:58*pm, acehr...@gmail.com wrote:
Quote:
On Jul 21, 1:27*pm, eran <kale...@gmail.comwrote:
>
Quote:
- Use a params struct
>
... that has a constructor to set all the members.
>
But then, values will have to be copied into the struct and then again
into the class.

I'd recommend using getters/setters the way they are implemented in
FLTK, for example, using parameter-based method overriding:

instance.varName (); /* returns the value */
instance.varName (value); /* sets the value */

As for checking if all the parameters are set, you should set default
values in the constructor, and before beginning to use the parameters
check if they have been set by the class's user.

Hope that helps
acehreli@gmail.com
Guest
 
Posts: n/a
#3: Jul 21 '08

re: Re: Trick needed - passing a large number of parameters to a class


On Jul 21, 2:31*pm, khalid...@gmail.com wrote:
Quote:
On Jul 21, 11:58*pm, acehr...@gmail.com wrote:
>
Quote:
On Jul 21, 1:27*pm, eran <kale...@gmail.comwrote:
>
Quote:
Quote:
- Use a params struct
>
Quote:
... that has a constructor to set all the members.
>
But then, values will have to be copied into the struct and then again
into the class.
Not "have to" for two reasons:

- struct members may be references to existing objects

- the compiler may optimize away the copy
Quote:
I'd recommend using getters/setters the way they are implemented in
FLTK, for example, using parameter-based method overriding:
>
instance.varName (); /* returns the value */
instance.varName (value); /* sets the value */
[This is not related to this topic, and completely subjective, but I
strongly recommend against using the same name for getters and
setters. It looks good on paper but very confusing.]

Now, what happened to the concern of copying? Wouldn't the setter copy
the value?
Quote:
As for checking if all the parameters are set, you should set default
values in the constructor, and before beginning to use the parameters
check if they have been set by the class's user.
That method would first default construct and then assign. Since
assignment involves destructing and copying, I don't think you are
really concerned about the cost of the method I proposed. Maybe I
misunderstood you. Why did you object using a constructor to set the
parameters?

Additionally, you method requires that every member must be
assignable, which not every type are.
Quote:
Hope that helps
Sure, but that is inferior to using a constructor. Besides, your
method does not address the OP's requirement at all: eran said "I'd
really like to minimize the chance that the caller will miss setting
one of the params."

Ali
Ian Collins
Guest
 
Posts: n/a
#4: Jul 22 '08

re: Re: Trick needed - passing a large number of parameters to a class


khalid302@gmail.com wrote:
Quote:
On Jul 21, 11:58 pm, acehr...@gmail.com wrote:
Quote:
>On Jul 21, 1:27 pm, eran <kale...@gmail.comwrote:
>>
Quote:
>>- Use a params struct
>... that has a constructor to set all the members.
>>
>
But then, values will have to be copied into the struct and then again
into the class.
>
Who copy? The class could work directly on the data.
Quote:
I'd recommend using getters/setters the way they are implemented in
FLTK, for example, using parameter-based method overriding:
>
Which is always the sign of a bad design. How do you make sure they are
all called or which ones upset the state of the class?

--
Ian Collins.
Closed Thread


Similar C / C++ bytes