I'm a Java developer trying to pick up C++. Is it okay to use a setter inside a constructor in order to reuse the sanity checks the setter provides?
For example:
#include <stdexcept>
using namespace std;
class Test {
private:
int foo;
void setFoo(int foo) {
if (foo < 42) {
throw invalid_argument{"Foo < 42."};
}
this->foo = foo;
}
public:
Test(int foo) {
setFoo(foo);
};
};
unsignedtypes exist to get rid of the test here.unsignedinstead? Then the compiler will handle the check at the time of compilation for you, instead of you needing a runtime check.unsignedstuff is not undisputed; see e.g. channel9.msdn.com/Events/GoingNative/2013/… 9:50, 42:40, 1:02:50 (Panel in which several prominent members of the committee argue against using unsigned integers for stuff other than bit-fiddling.)