Daniel,
Thanks for your response.
I'm interested in the technique of validation.
With the approach in my OP,
I would write the following
Any feedback (even pointing out obvious carelessness)
would be helpful.
It has been tested as below but testing has been limited.
Since it's been copy-pasted from my compiler, apologies in advance
if the line returns look ugly.
Many thanks. It's been a very helpful newsgroup for me.
Paul
#include <exception>
#include <iostream>
class ValidInteger
{
static constexpr int min = 0;
static constexpr int max = 1000; // Suppose more than 1000 trials is unfeasible.
int NumTrials;
public:
ValidInteger(int);
bool operator< (const ValidInteger&) const;
ValidInteger& operator++();
};
ValidInteger::ValidInteger(int numTrials) : NumTrials(numTrials)
{
if(NumTrials < min)
throw(std::runtime_error("Underlying value too small"));
if(NumTrials > max)
throw(std::runtime_error("Underlying value too large"));
}
bool ValidInteger::operator<(const ValidInteger& rhs) const
{
return NumTrials < rhs.NumTrials;
}
ValidInteger& ValidInteger::operator++()
{
++NumTrials;
return *this;
}
void f()
{
std::cout << "\n f is called\n";
}
void perform_f(ValidInteger trialsNum)
{
for(ValidInteger i = 0; i <trialsNum; ++i)
f();
}
int main() // test valid and invalid cases
{
try{
perform_f(5);
perform_f(-5);
}
catch (const std::runtime_error& e)
{
std::cout << "\n" << e.what() << "\n";
}
}