int operator[](int pos)const;与int& operator[](int pos);的区别

本文介绍了C++中下标运算符两种重载方式的区别:返回引用和返回值。通过具体示例展示了如何实现这两种重载,并解释了它们在不同场景下的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

int operator[](int pos)const;与int& operator[](int pos);的区别
搜索反回类型为引用的下标运算符一般在使用时用作左值,比如c[1]=1;在这种情况下,必须要求c[1]的结果是一个可以赋值的左值,因此int& operator[](int pos);这个函数的版本主要是为左值设定的,反回对变量的引用就可以作为左值。

int operator[](int pos)const;这个版本要注意,const一定要在后面,表示这个函数不能修改修改类中的变量,同时写在后面也表示这是前一个函数int& operator[](int pos);的重载版本,这样在调用语句比如c[1]时就不会出现二义性错误。这个版本的函数主要是用于下标运算符作为右值时使用的,但一般情况下用不到,比如b=a[1];这时同样是调用的int& operator[](int pos);版本的函数,除非变量c是const类型的。

在你给出的示例中,重写这两个函数都很简单,只需return p_arr[pos]即可,或者在反回之前加上对数组下标的边界检查就行了。下面举个用法

// ggg.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class CArray
{
private:
int *p_arr;
int size;
public:
CArray()
{ 
int a[3]={111,222,333};
p_arr=new int[3];
for(int i=0;i<3;i++) 
p_arr[i]=a[i];
size=3;
}

int operator[](int pos)const;//访问数组元素值的下标运算符重载函数

int& operator[](int pos) ; //设置数组元素值的下标运算符重载函数
};

int CArray::operator[](int pos)const {

cout<<"A"<<endl; 
if(pos<0 || pos>=size){
cout <<"Error!\n";
exit(1);
}
return p_arr[pos];

}

int& CArray::operator[](int pos){
cout<<"B"<<endl;

if(pos<0 || pos>=size){
cout <<"Error!\n";
exit(1);
}
return p_arr[pos];

}

void main()
{ int a=1;
CArray c;
const CArray b;

/*以下两个语句都会调用int& CArray::operator[](int pos)版本的重载函数*/
c[1]=2; /*这里会输出B */
a=c[1]; /*这里会输出B,说明调用的是第int &CArray::operator[](int pos)版本的函数 */

/*以下语句会调用int CArray::operator[](int pos)const 版本的重载函数*/

a=b[1]; /*这里会输出A,说明调用的是第int CArray::operator[](int pos)const版本的函数 */

cout<<c[0]<<endl; /*这里输出B之后输出111,因为使用了[]运算符,所以会调用重载的下标运算符函数*/
cout<<c[1]<<endl; /*这里输出B之后输出2,因为使用了[]运算符,所以会调用重载的下标运算符函数*/
cout<<a<<endl;
system("pause");
}
[选做]下面是一个数组类CArray的定义。要求: (1)在此基础上增加print()成员函数打印数组, (2)重载“=”、“+”、“-” 运算符使之能对该数组类对象进行赋值、加减运算。 (3)写出主函数对该类进行测试。 class CArray {private: int* p_arr; int size; public: CArray(); //缺省构造函数 CArray(int* p_a,int s); //构造函数 CArray(const CArray &r_other);//复制构造函数 ~CArray();//析构函数 int operator[](int pos) const; //访问数组元素值的下标运算符重载函数 int&amp; operator[](int pos); //设置数组元素值的下标运算符重载函数 CArray &operator=(const CArray &other)//赋值运算符“=”重载函数 CArray operator+(const CArray &other) //加运算符“+”重载函数 CArray operator-(const CArray &other) //减运算符“-”重载函数 void print() const; }; CArray:: CArray() { p_arr=NULL; size=0;} CArray:: CArray(int* p_a,int s) { if(s>0) { size=s; p_arr=new int[size]; for(int i=0;i<size;i++) p_arr[i]=p_a[i]; } Else { p_arr=NULL; size=0; } } CArray::CArray(const CArray &r_other) { size=r_other.size; if(size) { p_arr=new int[size]; for(int i=0;i<size;i++) p_arr[i]=r_other.p_arr[i]; } } CArray::~CArray() { if(p_arr) delete[] p_arr; p_arr=NULL; size=0; } int CArray::operator[](int pos) const { if(pos>=size) return p_arr[size-1]; if(pos<0) return p_arr[0]; return p_arr[pos]; } int&amp; CArray::operator[](int pos) { if(pos>=size) return p_arr[size-1]; if(pos<0) return p_arr[0]; return p_arr[pos]; }
06-03
namespace cl { //模拟实现list当中的结点类 template<class T> struct _list_node { //成员函数 _list_node(const T& val = T()); //构造函数 //成员变量 T _val; //数据域 _list_node<T>* _next; //后继指针 _list_node<T>* _prev; //前驱指针 }; //模拟实现list迭代器 template<class T, class Ref, class Ptr> struct _list_iterator { typedef _list_node<T> node; typedef _list_iterator<T, Ref, Ptr> self; _list_iterator(node* pnode); //构造函数 //各种运算符重载函数 self operator++(); self operator--(); self operator++(int); self operator--(int); bool operator==(const self& s) const; bool operator!=(const self& s) const; Ref operator*(); Ptr operator->(); //成员变量 node* _pnode; //一个指向结点的指针 }; //模拟实现list template<class T> class list { public: typedef _list_node<T> node; typedef _list_iterator<T, T&, T*> iterator; typedef _list_iterator<T, const T&, const T*> const_iterator; //默认成员函数 list(); list(const list<T>& lt); list<T>& operator=(const list<T>& lt); ~list(); //迭代器相关函数 iterator begin(); iterator end(); const_iterator begin() const; const_iterator end() const; //访问容器相关函数 T& front(); T& back(); const T& front() const; const T& back() const; //插入、删除函数 void insert(iterator pos, const T& x); iterator erase(iterator pos); void push_back(const T& x); void pop_back(); void push_front(const T& x); void pop_front(); //其他函数 size_t size() const; void resize(size_t n, const T& val = T()); void clear(); bool empty() const; void swap(list<T>& lt); private: node* _head; //指向链表头结点的指针 }; }
最新发布
08-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值