title: overload operator[] on return type
body: <p>There is something that is troubling my brain since a moment: I am trying to overload the [] operator based on the return type. Here is what I need to do:</p>
  
  <pre><code>class A {
  
  private:
      double* data_;
      int N_;
  public:
      A (N=0):N_(N){
          data_ = new double[N];
      }
      ~A {delete[] data_;}
  
      double operator[] (const int i) {
          return data_[i];
      }
  
      double* operator[] (const int i) {
          return &amp;data[i]; // for example; in fact here i need to return some block of data_ 
      }
  };
  </code></pre>
  
  <p>This code won't compile; and that is my problem. Can someone help me to solve this problem?</p>
  
  <p>PS: I know how to overload normal functions on the return type for example:</p>
  
  <pre><code>int foo ();
  string foo ();
  </code></pre>
  
  <p>I used some tricks that I read in this forum. In this way:</p>
  
  <pre><code>struct func {
      operator string() { return "1";}
      operator int() { return 2; }
  };
  
  int main( ) {
      int x    = func(); // calls int version
      string y = func(); // calls string version
      double d = func(); // calls int version
      cout &lt;&lt; func() &lt;&lt; endl; // calls int version
      func(); // calls neither
  }
  </code></pre>
  
  <p>Thank you.</p>