const重载operator []函数及其调用 [英] const overloaded operator[] function and its invocation

查看:121
本文介绍了const重载operator []函数及其调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类数组中定义了两个版本的重载 operator [] ptr 是指向数组对象的第一个元素的指针。

  int& array :: operator [](int sub){
return ptr [sub];
}

  int array :: operator [](int sub)const {
return ptr [sub]
}

现在,如果我定义一个const对象 integer1 第二个函数只能被调用.....但是如果我使一个非const对象然后调用如下:

  cout<< 第三值是< integer1 [2]; 

这里调用哪个函数?

解决方案

在第二个示例中,将调用非const版本,因为不需要转换,并且不需要转换的调用比需要转换的调用更好。 p>

然而,最终,你有一个基本的问题:你真正想要的是行为,改变取决于你使用你的对象作为右值或左值, code> const 不会真的这样做。为了使其正常工作,通常需要返回一个代理对象,并重载 operator = 运算符T 代理对象:

 模板< class T& 
class myarray {
T * ptr;

class proxy {
T& val;
proxy& operator =(proxy const& p); //分配不允许。
public:
proxy(T& t):val(t){}
operator T()const {return val; }
proxy& operator =(T const& t){val = t; return * this; }
};

代理const运算符[](int sub)const {return proxy(ptr [sub]); }
proxy operator [](int sub){return proxy(ptr [sub]); }
//显然其他东西像ctors需要。
};

现在我们得到正确的行为 - 当/如果我们的数组< int& (或任何类型)是const,我们的 operator [] const 将被使用,它会给出一个 const代理。由于赋值操作符不是 const,所以尝试使用它们将失败(不会编译)。



OTOH,如果原来的 array< int> 不是const,我们将得到一个非const的代理,在这种情况下,我们可以使用 operator T operator = ,并且能够读取和写入数组< int> P>

I define two versions of overloaded operator[] function in a class array. ptr is a pointer to first element of the array object.

int& array::operator[] (int sub) {  
   return ptr[sub];
} 

and

int array::operator[] (int sub) const { 
  return ptr[sub];
}

Now, if I define a const object integer1 the second function can only be called..... but if I make a non-const object and then invoke as below:

cout << "3rd value is" << integer1[2];

which function is called here?

解决方案

In your second example, the non-const version will be called, because no conversion is required, and a call that requires no conversion is a better match than one that requires a conversion.

Ultimately, however, you have a basic problem here: what you really want is behavior that changes depending on whether you're using your object as an rvalue or an lvalue, and const doesn't really do that. To make it work correctly, you normally want to return a proxy object, and overload operator= and operator T for the proxy object:

template <class T>
class myarray { 
    T *ptr;

    class proxy { 
        T &val;
        proxy &operator=(proxy const &p); // assignment not allowed.
    public:
        proxy(T &t) : val(t) {}
        operator T() const { return val; }
        proxy &operator=(T const&t) { val = t; return *this; }
    };

    proxy const operator[](int sub) const { return proxy(ptr[sub]); }
    proxy operator[](int sub) { return proxy(ptr[sub]); }
    // obviously other stuff like ctors needed.
};

Now we get sane behavior -- when/if our array<int> (or whatever type) is const, our operator[] const will be used, and it'll give a const proxy. Since its assignment operators are not const, attempting to use them will fail (won't compile).

OTOH, if the original array<int> was not const, we'll get a non-const proxy, in which case we can use both operator T and operator=, and be able to both read and write the value in the array<int>.

这篇关于const重载operator []函数及其调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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