STL vector 模板将元素访问器定义为const和non-const变体,例如:
reference operator[](size_type __n)
{return *(this->_M_impl._M_start + __n);}
const_reference operator[](size_type __n) const
{return *(this->_M_impl._M_start + __n);}
编译器何时决定使用一个版本而不使用另一个版本? vector 本身未定义为const,元素也未存储在其中。因此,给出了两个功能:
A f(int i) const
{ return myVector[i]; }
A f(int i)
{ return myVector[i]; }
我的理解是,第一个将调用operator []的const版本并返回constA。第二个将调用非const版本并返回非const A?
对我来说,f()的第一个版本似乎是要编写的“正确”版本,因为该函数没有改变任何东西,但是对于调用者来说,它返回一个const A可能令人惊讶。当然,如果我想要一个const返回后,我应该将f()定义为:
const A f(int i) const //Note the extra const at the start
{ return myVector[i]; }
这会告诉谁在写调用者以期待const的返回。
那么多余的const是通过魔术出现的吗?如果我使用boost::ptr_vector而不是std::vector,那么额外的const会应用于什么?数据?指针?两个都?
最佳答案
这利用了函数重载规则中棘手的部分。首先,在方法原型(prototype)的右括号后面的cv限定词就像参数上的cv限定词一样,但是它们适用于隐式this
参数。在假设的C++变体中,您必须声明this
,原型(prototype)如下所示:
reference operator[](this_type this, size_type n);
const_reference operator[](const this_type this, size_type n);
因此,如果要在其上调用该方法的对象是
const
,则第二个重载是更接近的匹配,将被调用。如果不是,则将调用第一个重载。因此,如果您为const_reference
容器编制索引,您将获得const
;如果您为非reference
容器编制索引,则会获得const
。然后,const_reference
对象将其指向的容器强制为只读性质。有时
const_reference
与const reference
相同,有时则不同。对于更复杂的容器,reference
是具有非平凡代码的类,对于只读类型,该代码必须不同。该标准始终使用const_reference
,以便实现者可以在需要时自由地执行此操作。关于c++ - STL容器中的const和non-const,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4473981/