I already saw this thread, however the situation is totally different and I can't apply that solution to my problem.
I have the following constructor:
Fan::Fan(Id id, std::string name, Age age);
Where Id and Age are typedef'ed unsigned int and unsigned short.
This is given to me, so I know that I must use them, as most likely the tester of the assignment will try to use numbers bigger than int (in range of unsigned int / short).
Obviously, a Fan's id and age cannot be negative numbers.
In the example code (to compile against), a fan is created with numbers for id and age. I cannot use string there and check for a minus sign.
I thought it wouldn't compile when I entered negative age / id in my unit testing. However, it turned out it did compile, and it gave random values (most likely due to overflow reasons).
So to conclude - it is possible to call the constructor with negative values for the age and id, but in the constructor their values get 'thrashed' and random numbers appear, and it causes unintended behavior.
For example, this:
Fan* fan = new Fan(-1, "Name", 9);
Compiles, and at run-time the id of the fan gets some unrelated value. So I must be able to detect the negative number in the constructor.
How do I "block" the negative numbers in the constructor?
Thanks for your time! I hope my question is clear.