数组订阅:返回引用与代理类方法 [英] Array Subscription: returning Reference vs proxy class method

查看:87
本文介绍了数组订阅:返回引用与代理类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在寻找为模板类重载Subscript('[]')运算符的方法时,我遇到了两种不同的技术.

While searching for methods for overloading Subscript('[]') operator for template class, I came across two different techniques.

第一种技术:

直接将operator []返回指针重载到容器,这将允许读取值和分配值.此技术的示例实现:

Overloading the operator [] returning pointer to the container directly, which will allow both reading value and assigning value. A sample implementation of this technique:

template <class T>
class X
{
    int _size;
    T *container;
public:
    X(int sz)
    {
        _size=sz;
        container=new T[sz]();
    }
    ~X()
    {

    }

    T& operator [](int indx)
    {
        return container[indx];
    }

};

main()为:

X<int> sample(100);
cout<<sample[9]<<endl;
sample[9]=9;
cout<<sample[9]<<endl;

输出:

0
9

第二种方法:

第二种技术涉及声明一个代理类,并通过该类重载operator =.此技术的示例实现:

Second technique involves declaring a proxy class and overloading the operator = via that class. A sample implementation of this technique:

template <class T>
class X
{
    int _size;
    T *container;
public:
    X(int sz)
    {
        _size=sz;
        container=new T[sz]();
    }
    ~X()
    {

    }

    class Proxy
    {
        int indx;
        X<T> &parent;
    public:
        Proxy(X<T> &p, int x) : parent(p),indx(x)
        {

        }
        void operator =(T assgn)
        {
            parent.container[indx]=assgn;
        }
        operator T const &()
        {
            return parent.container[indx];
        }
        friend class X<T>;//unnecessary line, I know!
    };
    Proxy operator[](int indx)
    {
        return Proxy(*this,indx);
    }

};

使用相同的main(),我们将获得相同的输出.

With same main() we get same output.

我个人喜欢第二种方法.但是,我真的很想比较这两种方法.这两种技术的主要功能区别是什么?这些方法各自有什么优势?

I personally like the second method. But, I really want to compare these two methods. What are the major functional difference of these two techniques. What advantages each of these method have?

推荐答案

如果您要公开未按原样存储的元素序列(需要从和进行转换,则可以使用您描述的基于代理的技术)存储空间),或者不能简单地通过引用进行访问.一个示例是std :: vector< bool>:在存储的每个字节(每个位一个)中打包八个bool.这样存储它们,就不可能返回对单个此类布尔的引用,因此索引运算符将返回代理对象"来支持对所包含布尔的读取和写入.

The proxy-based technique you describe can be used if you want to expose a sequence of elements that are not stored as-such (requiring a conversion from and to storage) or that cannot simply be accessed by-reference. An example is std::vector < bool >: it packs eight bools in every byte (one in each bit) in storage. Storing them that way, it is not possible to return a reference to a single such bool, so the index operator returns a "proxy-object" instead to support reading and writing of the contained bools.

如果可以返回对存储对象的直接引用,则除非希望限制分配(例如仅允许在容器中使用正值),否则将其包装在代理中并没有真正的优势. ).

If you can return a direct reference to the stored objects, there is no real advantage to wrapping it in a proxy unless you want to restrict the assignment (only allow positive values in the container for example).

这篇关于数组订阅:返回引用与代理类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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