Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 重载下标运算符“;[……”是;在l值和r值的情况下_C++_Operator Overloading_Subscript Operator - Fatal编程技术网

C++ 重载下标运算符“;[……”是;在l值和r值的情况下

C++ 重载下标运算符“;[……”是;在l值和r值的情况下,c++,operator-overloading,subscript-operator,C++,Operator Overloading,Subscript Operator,我在类中重载了[]操作符Interval以返回分钟或秒 但我不知道如何使用[]运算符为分钟或秒赋值 long& operator[](int index); // non-const function returning reference long const& operator[](int index) const;// const function returning const reference 例如:我可以使用此语句 cout通过引用返回,以便能够分配值并在赋值

我在类中重载了[]操作符Interval以返回分钟

但我不知道如何使用[]运算符为分钟赋值

long& operator[](int index);  // non-const function returning reference

long const& operator[](int index) const;// const function returning const reference
例如:我可以使用此语句


cout通过引用返回,以便能够分配值并在赋值运算符的LHS上使用它们。

重载op[]以使用硬编码的“索引”值在这里没有意义,实际上您的类定义中已经有了解决方案:

cout << a.minutes << "min and " << a.seconds << "sec" << endl;
long& operator[](int index);  // non-const function returning reference

long const& operator[](int index) const;// const function returning const reference

cout通过删除
const
并返回引用来更改函数签名:

long& operator[](int index)
现在,您将能够编写如下语句:

a[0] = 12;

将方法转换为如下所示的方法应该可以做到这一点:

long& operator[](int index) 

返回对相关成员的引用,而不是其值:

long &operator[](int index)
{
    if (index == 0)
        return seconds;
    else
        return minutes;
}

您的数组索引成员运算符应提供为

long& operator[](int index);                    // for non const object expressions

long const& operator[](int index) const;        // for const object expressions

为了避免子脚本运算符重载时出现混淆,建议使用子脚本运算符的
const
non-const
版本

long& operator[](int index);  // non-const function returning reference

long const& operator[](int index) const;// const function returning const reference
使用
A[1]=5
,您正试图修改
索引1
处的对象。因此,子脚本操作符的非常量版本将被自动调用


<> >代码>我是C++初学者,请你给我举个例子吧?刚刚注意到Vijay已经添加了片段。请参考。通常在C++中,如果您希望使用由您的Office操作符函数在LHS赋值中返回的值,那么您应该在OrrdIDEN运算符函数中引用引用。我知道它没有任何意义,但如果我想使用[]运算符赋值,那么我该怎么办?@ SeRoCK:另一个答案是:但是我不推荐它作为这个问题的解决方案。@Roger Pate+1我知道这是一个非常愚蠢的例子,但只是为了澄清我的疑问,我的夫人告诉我,重载[]运算符进行赋值是不可能的。不要忘记常量重载op[]。也就是说,不要删除现有的一个。加上这个,这看起来像是一个可怕的操作符重载的例子。你是否有一些模糊的要求迫使你这么做?否则这只是代码混淆。@jalf我知道这是一个很糟糕的例子,但我想在对象数组的泛型类中重载[]运算符。@jalf:为什么在没有任何编译器错误的情况下工作?编译器不应该抛出错误吗?它不提供任何输出。这个节目到底发生了什么。
long& operator[](int index);  // non-const function returning reference

long const& operator[](int index) const;// const function returning const reference