C++关于运算符[]的重载问题

AI小男孩 2014-05-24 11:26:47
定义一个数组类Array,在类中重载两个[ ]运算符,如何让其中一个作左值,让另一个作右值?
...全文
157 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
点不亮的黑色 2014-05-25
  • 打赏
  • 举报
回复
引用 3 楼 LinuxTiger 的回复:
[quote=引用 2 楼 u012954083 的回复:] 好像没什么用,我用下面的程序试了一下,在断点调试的时候只会调用int& operator[](int subscript)这个函数
首先,就没有什么所谓的左值运算符,右值运算符。 其次,你若是想让程序进入const int operator[],你只需要声明成const Array integers1(num1); 而且,你不理解左值和右值的概念。 不是写在左边的叫左值,写在右边的叫右值。 左值是一个引用到具体变量的对象,这个对象不是临时的,可以读取,可以存储。 右值是内存中的某个数据,生命周期短,往往是只读的。 关于右值的概念,你可以参考C++ 11 ISO 文档3.10 的第一条。
引用
An rvalue (so called, historically, because rvalues could appear on the right-hand side of an assignment expression) is an xvalue, a temporary object (12.2) or subobject thereof, or a value that is not associated with an object.
从定义里可以清楚的看到,rvalue只有三种情况:临时对象、子对象、字面值。 你这个表达式int ss = integers1[0]; integers1[0]不是右值,右值是integers1[0]里面存储的数据。 [/quote]厉害
AI小男孩 2014-05-25
  • 打赏
  • 举报
回复
[/quote]
引用 3 楼 LinuxTiger 的回复:
[quote=引用 2 楼 u012954083 的回复:] 好像没什么用,我用下面的程序试了一下,在断点调试的时候只会调用int& operator[](int subscript)这个函数
首先,就没有什么所谓的左值运算符,右值运算符。 其次,你若是想让程序进入const int operator[],你只需要声明成const Array integers1(num1); 而且,你不理解左值和右值的概念。 不是写在左边的叫左值,写在右边的叫右值。 左值是一个引用到具体变量的对象,这个对象不是临时的,可以读取,可以存储。 右值是内存中的某个数据,生命周期短,往往是只读的。 关于右值的概念,你可以参考C++ 11 ISO 文档3.10 的第一条。
引用
An rvalue (so called, historically, because rvalues could appear on the right-hand side of an assignment expression) is an xvalue, a temporary object (12.2) or subobject thereof, or a value that is not associated with an object.
从定义里可以清楚的看到,rvalue只有三种情况:临时对象、子对象、字面值。 你这个表达式int ss = integers1[0]; integers1[0]不是右值,右值是integers1[0]里面存储的数据。 [/quote] 谢谢指教,学习了
对象 2014-05-25
  • 打赏
  • 举报
回复
引用 2 楼 u012954083 的回复:
好像没什么用,我用下面的程序试了一下,在断点调试的时候只会调用int& operator[](int subscript)这个函数
首先,就没有什么所谓的左值运算符,右值运算符。 其次,你若是想让程序进入const int operator[],你只需要声明成const Array integers1(num1); 而且,你不理解左值和右值的概念。 不是写在左边的叫左值,写在右边的叫右值。 左值是一个引用到具体变量的对象,这个对象不是临时的,可以读取,可以存储。 右值是内存中的某个数据,生命周期短,往往是只读的。 关于右值的概念,你可以参考C++ 11 ISO 文档3.10 的第一条。
引用
An rvalue (so called, historically, because rvalues could appear on the right-hand side of an assignment expression) is an xvalue, a temporary object (12.2) or subobject thereof, or a value that is not associated with an object.
从定义里可以清楚的看到,rvalue只有三种情况:临时对象、子对象、字面值。 你这个表达式int ss = integers1[0]; integers1[0]不是右值,右值是integers1[0]里面存储的数据。
ningto.com 2014-05-24
  • 打赏
  • 举报
回复

    const char operator[](int i) const;
    char& operator[](int i);
    const char operator[](int i) const;
    char& operator[](int i);
AI小男孩 2014-05-24
  • 打赏
  • 举报
回复
好像没什么用,我用下面的程序试了一下,在断点调试的时候只会调用int& operator[](int subscript)这个函数

#include<iostream>
using namespace std;

class Array {
    public:
        Array(int a = 0) {
             size = a;   
             ptr = new int[size];
             for (int i = 0; i < size; ++i) {
                  *(ptr+i) = 0;
             }
        }
       ~Array() {
            delete [] ptr;
	   }
        int getSize() const {
            return size;
       }
       int& operator[](int subscript) {
    		return ptr[subscript];
       }
	   const int operator[](int subscript) const {
	        return ptr[subscript];
       }
    private:
        int size;
        int *ptr;
}; 

int main() {
    int num1;
    cin >> num1;
    Array integers1(num1);
    cout << integers1[0] << endl;
    integers1[0] = 1000;
    int ss = integers1[0];
    return 0;
}

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧