I'm trying to design a hierarchy classes that extends the integers C types. What I'm trying to achieve is basically the following:
I have a basic class called Integer
this class is an abstract class, the only thing I wish it would provide is basically the basic operations between integers, namely +,-,*,/,&,^,|,~,etc if I'm missing something
, just the interface no implementation. Then I would subclass such class with StaticInteger
and DynamicInteger
the difference is that the StaticInteger
uses templates to describes the size of the type at compile time, while DynamicInteger
the size can be changed at run time.
Some use case would be the following for both classes:
StaticInteger<14> is1 = 3;
StaticInteger<32> is2 = 15;
StaticInteger<16> is3 = is1 + is2;
DynamicInteger id1;
id1.setWidth(14);
id1.setWidth(16);
id1 = is1;
Internally the data is represented by using arrays, so for example the resizing in the dynamic type would be achieved using new/delete
operators;
Basically what puzzles me is how to properly design the interface of the base abstract class Integer. Let's say I would design straightly the class StaticInteger I woudl try to do something like:
template <int L>
class StaticInteger {
public:
//bla bla
template <int M>
StaticInteger<L> operator+(StaticInteger<M> x);
private:
//bla bla
};
The dynamic instead would be
class DynamicInteger {
public:
DynamicInteger operator+(DynamicInteger x);
private:
};
but how would I specify the interface that would cover any possible interface of such operator? Between static and dynamic I think is clear that in general for a given operator the algorithm implementation could differ.
Just a further note, I do understand there's probably a philosophical problem behind what I'm trying to do, since an operator is actually a function in C++, and to be properly defined it requires all the types specified a priory, in the base class.
Update : Just an alternative design, I was thinking maybe I could abstract the concept of operations using a separate class (like a strategy pattern).