You don't need to have to have a virtual destructor unless you do dynamic allocation and delete
via a pointer to base class.
The real problem lies at the design level. For while it is true that every PositiveLiteral
value is a Literal
value, if you have a reference to a Literal
which is really a PositiveLiteral
, then you can assign it a negative value…
In the literature and forum discussions this was once known as the ellipse-versus-circle problem, although the similarity isn't obvious.
First, to be very clear about the problem, it's only for immutable values that a PositiveLiteral
is a Literal
. It is not the case that a mutable PositiveLiteral
is a mutable Literal
.
Then, a practical C++ solution is to provide value conversion instead of using inheritance.
For example, this is the solution used for smart pointers.
Addendum: I failed to see that in the OP's code the value is const
. So there's no such problem.
A practical problem is that at least one compiler, Visual C++, has a tendency to silly-warn about its inability to generate a copy assignment operator; it can be shut up by declaring a private one without implementation. :-)