2025年4月10日 星期四 乙巳(蛇)年 正月十一 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > VC/VC++

C++重载[ ](下标运算符)

时间:01-05来源:作者:点击数:368

C++ 规定,下标运算符[ ]必须以成员函数的形式进行重载。该重载函数在类中的声明格式如下:

返回值类型 & operator[ ] (参数);

或者:

const 返回值类型 & operator[ ] (参数) const;

使用第一种声明方式,[ ]不仅可以访问元素,还可以修改元素。使用第二种声明方式,[ ]只能访问而不能修改元素。在实际开发中,我们应该同时提供以上两种形式,这样做是为了适应 const 对象,因为通过 const 对象只能调用 const 成员函数,如果不提供第二种形式,那么将无法访问 const 对象的任何元素。

下面我们通过一个具体的例子来演示如何重载[ ]。我们知道,有些较老的编译器不支持变长数组,例如 VC6.0、VS2010 等,这有时候会给编程带来不便,下面我们通过自定义的 Array 类来实现变长数组。

  • #include <iostream>
  • using namespace std;
  • class Array{
  • public:
  • Array(int length = 0);
  • ~Array();
  • public:
  • int & operator[](int i);
  • const int & operator[](int i) const;
  • public:
  • int length() const { return m_length; }
  • void display() const;
  • private:
  • int m_length; //数组长度
  • int *m_p; //指向数组内存的指针
  • };
  • Array::Array(int length): m_length(length){
  • if(length == 0){
  • m_p = NULL;
  • }else{
  • m_p = new int[length];
  • }
  • }
  • Array::~Array(){
  • delete[] m_p;
  • }
  • int& Array::operator[](int i){
  • return m_p[i];
  • }
  • const int & Array::operator[](int i) const{
  • return m_p[i];
  • }
  • void Array::display() const{
  • for(int i = 0; i < m_length; i++){
  • if(i == m_length - 1){
  • cout<<m_p[i]<<endl;
  • }else{
  • cout<<m_p[i]<<", ";
  • }
  • }
  • }
  • int main(){
  • int n;
  • cin>>n;
  • Array A(n);
  • for(int i = 0, len = A.length(); i < len; i++){
  • A[i] = i * 5;
  • }
  • A.display();
  • const Array B(n);
  • cout<<B[n-1]<<endl; //访问最后一个元素
  • return 0;
  • }

运行结果:

5↙
0, 5, 10, 15, 20
33685536

重载[ ]运算符以后,表达式arr[i]会被转换为:

arr.operator[ ](i);

需要说明的是,B 是 const 对象,如果 Array 类没有提供 const 版本的operator[ ],那么第 60 行代码将报错。虽然第 60 行代码只是读取对象的数据,并没有试图修改对象,但是它调用了非 const 版本的operator[ ],编译器不管实际上有没有修改对象,只要是调用了非 const 的成员函数,编译器就认为会修改对象(至少有这种风险)。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门