I think it’s easier to explain with an example. Let’s take a class that models the speed of a Formula 1 car, the interface may look something like this:
class SpeedF1
{
public:
explicit SpeedF1(double speed);
double getSpeed() const;
void setSpeed(double newSpeed);
private:
double speed_;
};
Now a negative speed does not make sense in this particular case and no value is greater than 500 km / h. In this case, the constructor and the setSpeed function may throw exceptions if the supplied value is not within the logical range.
I can introduce an additional layer of abstraction and insert an additional object instead of a double one. The new object will be a wrapper around the double, and it will be created and will never be changed. The class interface will be changed to:
class ReasonableSpeed
{
public:
explicit ReasonableSpeed(double speed);
double getSpeed() const;
private:
double speed_;
};
class SpeedF1
{
public:
explicit SpeedF1(const ReasonableSpeed& speed);
ReasonableSpeed getSpeed() const;
void setSpeed(const ReasonableSpeed& newSpeed);
private:
ReasonableSpeed speed_;
};
SpeedF1 , , reset .
, (, ), . , , , .
, :
?