constraining class method parameters?

Apr 16, 2020 at 4:53pm
Hi everybody, I'm new here and to c++ in general, so please consider me a total novice.

I'm in the process of learning c++; as an exercise I'm writing a simple class and a user program that uses it.

The class has one public method that processes a double (passed as parameter) and updates a couple of public member variables according to it.

The parameter passed to the method is going to be constrained (by specs) to a specific interval, let's say: between -10.0 and +10.0. Values outside this range are to be considered invalid, meaningless.

So I'm wondering what's best practice for c++ in such cases; what the possible approaches are when dealing with unacceptable argument values passed to a function and so on. Is there any preferred way to deal with it?

I'm going to validate user input (before calling the above mentioned method) anyway, so this is not critical to me, but I'd prefer to write this class in a way that its method doesn't return meaningless values in the first place nonetheless.

Consider I'm using this code only to learn c++, I'm not going to use it into any major project or similar. It's ment to be used as a training platform only.
Apr 16, 2020 at 5:04pm
It is hard to say what value someone might pass as an argument, so my approach would be to allow any value to pass into the function and have code inside the function that ignores the out of range argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;


void test(double d) {
 if(d >= -10.0 && d <= 10.0) {
     cout << "hi";
     //doing something usefull
 }
}

int main()
{
  double mydouble = 3.0;
  
  test(mydouble);
  
  mydouble = -11.0;
  
  test(mydouble);
  
  return 0;
  
}
Apr 16, 2020 at 5:13pm
The most important thing to do here is to document how you handle it. The way you actually handle the issue actually matters less. What's important is that whoever calls the function knows that to expect.

Now, here are some things that you could do in the code. Which one you use depends on what seems most appropriate for the actual class.

1. "Don't do that." You could simply require that the caller pass valid data. Since you've already validated the input this is a reasonable thing to do.
2. Return an error status. Check for invalid data and return an error status if the data is bad. So the method might be:
1
2
3
// Process the number. Returns true on success, otherwise return false and leave
// the class instance unchanged.
bool MyClass::process(double d);

3. Set a flag in the class indicating that it's invalid.
4. Exit the program.

And in all of these, you might also print an error message somewhere to say what the problem was. Include the function that was called, the parameter(s) that were passed in and the reason for the error.
Apr 18, 2020 at 11:16am
Thank you Dhayden and Manga, I will consider both suggestions :-)

I'm reading more and more in the meanwhile and I've encountered the concept of Error Exceptions, specifically I've found std::doamin_error ( http://www.cplusplus.com/reference/stdexcept/domain_error/ ) which seams to related to my case. What do you reckon?
Apr 18, 2020 at 12:54pm
I avoid throwing exceptions as much as possible. People tend to not catch them, sometimes out of laziness, sometimes because they don't realize that underlying code can throw.

Don't get me wrong, exceptions have their place and I've used them myself. I just prefer more explicit methods of error checking.
Topic archived. No new replies allowed.