On Jul 21, 2:31*pm, khalid...@gmail.com wrote:
On Jul 21, 11:58*pm, acehr...@gmail.com wrote:
On Jul 21, 1:27*pm, eran <kale...@gmail.comwrote:
- 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.
Not "have to" for two reasons:
- struct members may be references to existing objects
- the compiler may optimize away the copy
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?
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.
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