When developing a solution, it is sometimes convenient to provide wrapper classes for primitive data types. Consider a class that represents a numeric value, be it double , a float or int .
class Number { private: double val; public: Number(int n) : val(n) { } Number(float n) : val(n) { } Number(double n) : val(n) { }
Now suppose 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);
It sounds simple enough. But what if the function was like that?
void primitive_increment(int& n) { ++n; }
Note that increment is an example. It is assumed that the function will perform more complex operations with primitive data types, which they can also perform on Number types without any problems.
How will I use the function in the same way as before? How in:
Number n(2); primitive_increment(n);
How can I map the Number class to primitive_increment ? How to create a wrapper class for primitive data types that will be compatible wherever these data types are required?
So far, I have found only two solutions. One of them is to create a function such as double& Number::get_value() , and then use it as primitive_increment(n.get_value()); . The second solution is to create implicit conversion methods, such as Number::operator int&() ; but this can lead to many ambiguous calls and make the code confusing.
I am wondering if there is another solution for implementing these types of shells and preserving their primitive functions.
Update:
For further clarification in this project, the goal here is to create all data types derived from one base class, which is usually called Object when developing such a solution. The limitation is that no external library should be used. Therefore, if I have a container with pointers to the Object type, it should be able to hold any arbitrary value, primitive or not, and perform any primitive operation that is permitted on Object . Hope this explains it better.