基本数据类型的包装类 [英] Wrapper Classes for Primitive Data Types

查看:206
本文介绍了基本数据类型的包装类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计解决方案时,有时可能方便地为原始数据类型提供包装类。考虑一个表示数值的类,如 double float int

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);
}

我会使用这个函数:

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;
}

请注意,增量是一个例子。假设函数对原始数据类型执行更复杂的操作,他们也应该能够在 Number 类型上执行,而没有任何问题。

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);

如何使我的 Number primitive_increment ?如何为原始数据类型创建一个包装器类,以便在需要这些数据类型的任何位置兼容?

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?

到目前为止,我只找到两个解决方案。一个是创建一个函数,例如 double& Number :: get_value(),然后使用 primitive_increment(n.get_value()); 。第二个解决方案是创建隐式转换方法,例如 Number :: operator int&();但是这些可能会导致很多模糊的调用,并会使代码混淆。

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.

更新:

这里的目的是使所有的数据类型从一个基类派生,通常在设计这样的解决方案时被称为 Object 。约束是不应该使用外部库。因此,如果我有一个容器有指向 Object 类型的指针,它应该能够保存任何任意的值,不管是否是原语,并且执行任何允许的原始操作在对象

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有显式的运算符重载。

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
}

这篇关于基本数据类型的包装类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆