Overloading the subscript operator “[ ]” in the l-

2020-07-11 06:35发布

I have overloaded [] operator in my class Interval to return minutes or seconds.

But I am not sure how to assign values to minutes or second using [] operator.

For example : I can use this statement

cout << a[1] << "min and " << a[0] << "sec" << endl;

but I want to overload [] operator, so that I can even assign values to minutes or seconds using

a[1] = 5;
a[0] = 10;

My code :

#include <iostream>

using namespace std;

class Interval
{

public:

    long minutes;
    long seconds;

    Interval(long m, long s)
    {
        minutes = m + s / 60;
        seconds = s % 60;
    }

    void Print() const
    {
        cout << minutes << ':' << seconds << endl;
    }

    long operator[](int index) const
    {
        if(index == 0)
            return seconds;

        return minutes;
    }

};

int main(void)
{
    Interval a(5, 75);
    a.Print();
    cout << endl;

    cout << a[1] << "min and " << a[0] << "sec" << endl;
    cout << endl;

}

I know I have to declare member variables as private, but I have declared here as public just for my convenience.

7条回答
你好瞎i
2楼-- · 2020-07-11 06:39

Return a reference to the member in question, instead of its value:

long &operator[](int index)
{
    if (index == 0)
        return seconds;
    else
        return minutes;
}
查看更多
不美不萌又怎样
3楼-- · 2020-07-11 06:39

Change the function signature by removing the const and returning a reference:

long& operator[](int index)

Now you will be able to write statements like:

a[0] = 12;
查看更多
Deceive 欺骗
4楼-- · 2020-07-11 06:49

Your array index member operator should be provided as

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

long const& operator[](int index) const;        // for const object expressions
查看更多
手持菜刀,她持情操
5楼-- · 2020-07-11 06:51

converting the method to as given below should do it:

long& operator[](int index) 
查看更多
爷的心禁止访问
6楼-- · 2020-07-11 06:58

Overloading op[] to use hardcoded "index" values doesn't make sense here, and you actually already have the solution in your class definition:

cout << a.minutes << "min and " << a.seconds << "sec" << endl;

You can turn those into methods instead of public data members, that's inconsequential for not overloading op[]. However, since you want write access as well, the only advantage a method would have is validation (e.g. checking 0 <= seconds < 60).

struct Interval {
  int minutes() const { return _minutes; }
  void minutes(int n) { _minutes = n; }  // allows negative values, etc.

  int seconds() const { return _seconds; }
  void seconds(int n) {
    if (0 <= n and n < 60) {
      _seconds = n;
    }
    else {
      throw std::logic_error("invalid seconds value");
    }
  }

  // rest of class definition much like you have it

private:
  int _minutes, _seconds;
};

// ...
cout << a.minutes() << "min and " << a.seconds() << "sec" << endl;
查看更多
一夜七次
7楼-- · 2020-07-11 07:02

In-order to avoid confusion in the case of overloading the sub-script operator, it is recommended to use the const and non-const version of the sub-script operator.

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

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

With A[1] = 5, you are trying to modify the object at index 1. So the non-const version of the sub-script operator will be invoked automatically.

With cout << A[1], you are not modifying the object at index 1. So the const version of the sub-script operator will be invoked automatically.

查看更多
登录 后发表回答