title: Overloading multiple operators
body: <p>Concisely, my goal is to have foo[bar] return type1, and foo[bar]= return type2.</p>
  
  <p>I am writing an object in C++, and it's coming along quite nicely, however there's just one tiny little thing that I wish to do, yet it seems impossible.</p>
  
  <p>My object is a storage class, so I am using an array subscript to access values. I also need to assign, so I also overload the = operator. However, it is somewhat inconvenient because the values that my class holds are first class objects, so for my array subscript overload I can't return them as is. I have to return an intermediate class to handle the = operator, but I also want to retrieve the value without additional typing.</p>
  
  <p>Is there any way to do this? Hackish ways are acceptable.</p>
  
  <p>Edit: Here's an example of what it (should) do</p>
  
  <pre><code>#include&lt;cstdio&gt;
  #include&lt;cstdlib&gt;
  
  class foo{
      char* a[100];
      foo(){
          for( int i = 0; i &lt; 100; i ++)
              a[i] = 0;
      }
      char* operator[] (int location){
          return a[location];
      }
      foo&amp; operator[]= (int location, const char* value ){
          if( a[location] == 0 )
              a[location] = (char*) malloc( strlen( value ) + 1 );
          else
              a[location] = (char*) realloc( a[location], strlen( value ) + 1 );
          strcpy( a[location], value );
      }
  };
  int main(){
      foo bar;
      bar[20] = "Hello";
      printf( "bar[20] = %s\n", bar[20] );
      bar[20] = "Hello There";
      printf( "bar[20] = %s\n", bar[20] );
      printf( "bar[20][0] = %c\n", bar[20][0] );
  }
  
  Output:
  bar[20] = Hello
  bar[20] = Hello There
  bar[20][0] = H
  </code></pre>
  
  <p>Edit again: I think I'll try phrasing this in a different, but workable way. Is there a way to overload the return type when a class is referenced? Such that if I have </p>
  
  <pre><code>class foo{
      bool a;
      bool operator /*referenced*/ (){
          return a
      }
      foo&amp; operator=(bool b){
          a = b;
      }
  };
  int main(){
      foo a;
      a = b;
      if( a == b )
          printf("It Works!");
  }
  </code></pre>
  
  <p>that would actually work?</p>