Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapper Classes for Primitive Data Types

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.

like image 422
Zeenobit Avatar asked Nov 09 '11 00:11

Zeenobit


2 Answers

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
}
like image 195
Pubby Avatar answered Nov 15 '22 00:11

Pubby


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.

like image 44
Mooing Duck Avatar answered Nov 14 '22 22:11

Mooing Duck