
AMDG
Maarten Kronenburg wrote:
In my opinion an unsigned integer is a, works like a and can be substituted as an integer, and so is a modular integer, and as far as I'm concerned also an allocated integer. This is why I choose for derivation, and then with
base pointer to derived object (as quoted above) to runtime
"Steven Watanabe" wrote in message the polymorphism.
I strongly disagree.
1) An unsigned integer only works like an integer for non-mutating operations. If clients take parameters of const integer&, sure, unsigned_integer will work fine in this context. Treating non-const integers polymorphically is a very bad idea, though. Code that does so has to be very carefully designed to make sure that it can handle all the combinations. If you have to use inheritance, make both integer and unsigned_integer inherit from a common base.
In my design the integer class is the value class, which can also be used as data member of other (possibly template) classes, and the unsigned_integer class is polymorphically derived so that a base pointer to a derived object still works. So in my opinion this design has the best of both worlds. But there are of course many other ways to do it with other pros and cons. But in my opinion these days templates are used in designs by default, and that's not a good thing. Users cannot derived from the STL string because it has no virtual destructor, but for that class that scenario was not so important. For the class integer I think we cannot afford to overlook this. Don't look at me if somewhere in the future this will bubble up.
2) I would expect an integer class to be a value object. virtual functions don't play well with value objects, because to prevent slicing, everything has to be held by pointer.
As mentioned the integer base class can be used as value class, the only price is the unused vtable.
3) unsigned_integer + unsigned_integer should return an unsigned_integer. There is no way for the result to be negative. This is even more glaring for modular_integer
No, what about unsigned_integer - unsigned_integer? That can be negative!
And in my opinion an integer is not a container, so I see no reason to use templates.
???