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.
C++11 has explicit operator overloads.
struct silly_wrapper {
int foo;
explicit operator int&() { return foo; }
};
void primitive_increment(int& x) { ++x; }
int main()
{
silly_wrapper x;
primitive_increment(x); // works
x += 1; // doesn't work - can't implicitly cast
}
class Number {
enum ValType {DoubleType, IntType} CurType;
union {
double DoubleVal;
int IntVal;
};
public:
Number(int n) : IntVal(n), CurType(int) { }
Number(float n) : DoubleVal(n), CurType(DoubleType) { }
Number(double n) : DoubleVal(n), CurType(DoubleType) { }
// Assume copy constructors and assignment operators exist
Number& add(const Number& other) {
switch(CurType) {
case DoubleType: DoubleVal += other.to_double(); break;
case IntType: IntVal+= other.to_int(); break;
}
return *this;
}
int& to_int() {
switch(CurType) {
case DoubleType: IntVal = DoubleVal; CurType = IntType; break;
//case IntType: DoubleVal = IntVal; CurType = DoubleType; break;
}
return IntVal;
}
const int to_int() const {
switch(CurType) {
case DoubleType: return (int)DoubleVal;
case IntType: return (int)IntVal;
}
}
const float to_float() const {
switch(CurType) {
case DoubleType: return (float)DoubleVal;
case IntType: return (float)IntVal;
}
}
double& to_double() {
switch(CurType) {
//case DoubleType: IntVal = DoubleVal; CurType = IntType; break;
case IntType: DoubleVal = IntVal; CurType = DoubleType; break;
}
return DoubleVal;
}
const double to_double() const {
switch(CurType) {
case DoubleType: return (double)DoubleVal;
case IntType: return (double)IntVal;
}
}
};
void primitive_increment(int& n) {
++n;
}
int main() {
Number pi(3.1415);
primitive_increment(pi.to_int());
//pi now is 4
return 0;
}
I will admit this is quite awkward, and not the ideal situation, but it solves the given problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With