Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 21st, 2008, 09:05 PM
acehreli@gmail.com
Guest
 
Posts: n/a
Default Re: Trick needed - passing a large number of parameters to a class

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

  #2  
Old July 21st, 2008, 09:35 PM
khalid302@gmail.com
Guest
 
Posts: n/a
Default 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
  #3  
Old July 21st, 2008, 10:05 PM
acehreli@gmail.com
Guest
 
Posts: n/a
Default 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
  #4  
Old July 22nd, 2008, 05:35 AM
Ian Collins
Guest
 
Posts: n/a
Default 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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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,662 network members.