In designing a solution, sometimes it may be convenient to provide wrapper classes for primitive data types. Consider a class that represents a numeric value, be it a double
, a float
, or an int
.
class Number {
private:
double val;
public:
Number(int n) : val(n) { }
Number(float n) : val(n) { }
Number(double n) : val(n) { }
// Assume copy constructors and assignment operators exist
Number& add(const Number& other) {
val += other.val;
return *this;
}
int to_int() const { return (int) val; }
float to_float() const { return (float) val; }
double to_double() const { return val; }
};
Now suppose that I have a function as such:
void advanced_increment(Number& n) {
n.add(1);
}
And I would use this function as such:
Number n(2);
advanced_increment(n); // n = 3
This sounds easy enough. But what if the function was like this?
void primitive_increment(int& n) {
++n;
}
Note that the increment is an example. It is assumed that the function would perform more complicated operations on primitive data types that they should also be able to perform on Number
types without any issues.
How would I use the function exactly as before? As in:
Number n(2);
primitive_increment(n);
How could I make my Number
class compatible with primitive_increment
? How could I create a wrapper class for primitive data types that would be compatible anywhere that these data types are required?
So far, I have only found two solution. One is to create a function such as double& Number::get_value()
and then use it like primitive_increment(n.get_value());
. The second solution is to create implicit conversion methods such as Number::operator int&()
; but these can result in many ambiguous calls and would make the code confusing.
I'm wondering if there is any other solution to implement these types of wrappers and retain their primitive functionality.
Update:
To further clarify, in the actual project, the intent here is to make all data types derived from one base class that is commonly referred to as Object
when designing such solution. A constraint is that no outside library should be used. Therefore, if I have a container that has pointers to the type Object
, it should be able to hold any arbitrary value, primitive or not, and perform any primitive operation that is allowed on Object
. I hope this explains it better.
Number
class when passed intoprimitive_increment
? Specifically, what happens when it currently has a non-integer value (e.g. 2.5)? Or a value beyond the range ofint
s? Until you've decided the semantics, it's not possible to provide a proper answer to this question. – Uraniafloat
to yourprimitive_increment
function, so it's entirely unclear what behaviour you seek to emulate here! – UraniaNumber
should inherit fromObject
; for what purpose? Why not just wrap each primitive type in its own class, e.g.Float
,Double
,Int
? – UraniaFloat
,Double
, andInt
would be derived fromNumber
which would be derived fromObject
. I tried to simply the example in question, but I don't think it worked. :( – ViborgInt
class that can be used anywhere anint
can? – UraniaInt
class would offer additional functionality and allow the use of completely polymorphic data structures that can contain any type of data, primitive or not. – Viborgroost::any
. The for any elementx
you want to say++x
. This would result in a virtual function call to the implementation'soperator++
, which in turn would call a type-specific operation. It's just that the idea of wrapping primitive numeric types into such thing sounds so barbaric, but for more heavy-weight objects, that might be an idea. (For a bounded set of types, perhaps we could also makeroost::variable_type
.) – Sararoost::any
would be implemented? I took a look atboost:any
and I couldn't find anything that would point me in the right direction. – Viborg