C++: Define a Primitive Data Type

Bjarne-stroustrup
 

Demonstrate how to define a type that behaves like an integer but has a lowest valid value of 1 and a highest valid value of 10. Include all bounds checking you need to write, or explain how the compiler or interpreter creates those bounds checks for you.

This class relies on implicit conversions to do most int operations; however the combined operations with assignment have to be coded explicitly.

#include <stdexcept>

class tiny_int
{
public:
	tiny_int(int i):
	value(i)
	{
		if (value < 1)
		throw std::out_of_range("tiny_int: value smaller than 1");
		if (value > 10)
		throw std::out_of_range("tiny_int: value larger than 10");
	}
	operator int() const
	{
		return value;
	}
	tiny_int& operator+=(int i)
	{
		// by assigning to *this instead of directly modifying value, the
		// constructor is called and thus the check is enforced
		*this = value + i;
		return *this;
	}
	tiny_int& operator-=(int i)
	{
		*this = value - i;
		return *this;
	}
	tiny_int& operator*=(int i)
	{
		*this = value * i;
		return *this;
	}
	tiny_int& operator/=(int i)
	{
		*this = value / i;
		return *this;
	}
	tiny_int& operator<<=(int i)
	{
		*this = value << i;
		return *this;
	}
	tiny_int& operator>>=(int i)
	{
		*this = value >> i;
		return *this;
	}
	tiny_int& operator&=(int i)
	{
		*this = value & i;
		return *this;
	}
	tiny_int& operator|=(int i)
	{
		*this = value | i;
		return *this;
	}
private:
	unsigned char value; // we don't need more space
};

SOURCE

Content is available under GNU Free Documentation License 1.2.