title: Overloading the subscript operator "[ ]" in the l-value and r-value cases
body: <p>I have overloaded [] operator in my class <strong>Interval</strong> to return <strong>minutes</strong> or <strong>seconds</strong>.</p>
  
  <p>But I am not sure how to assign values to <strong>minutes</strong> or <strong>second</strong> using [] operator.</p>
  
  <p>For example : I can use this statement <br><br>
  <code>cout &lt;&lt; a[1] &lt;&lt; "min and " &lt;&lt; a[0] &lt;&lt; "sec" &lt;&lt; endl;</code></p>
  
  <p>but I want to overload [] operator, so that I can even assign values to minutes or seconds using</p>
  
  <pre><code>a[1] = 5;
  a[0] = 10;
  </code></pre>
  
  <p><strong>My code :</strong> </p>
  
  <pre><code>#include &lt;iostream&gt;
  
  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 &lt;&lt; minutes &lt;&lt; ':' &lt;&lt; seconds &lt;&lt; endl;
      }
  
      long operator[](int index) const
      {
          if(index == 0)
              return seconds;
  
          return minutes;
      }
  
  };
  
  int main(void)
  {
      Interval a(5, 75);
      a.Print();
      cout &lt;&lt; endl;
  
      cout &lt;&lt; a[1] &lt;&lt; "min and " &lt;&lt; a[0] &lt;&lt; "sec" &lt;&lt; endl;
      cout &lt;&lt; endl;
  
  }
  </code></pre>
  
  <p>I know I have to declare member variables as private, but I have declared here as public just for my convenience.</p>