title: Object as array index
body: <p>I have the following class:</p>
  
  <pre><code>class MyInteger
  {
  private:
      __int64 numero;
  
      static __int64 int64Pow(__int64, __int64);
  public:
      // It doesn't matter how these methods are implemented
      friend class MyInteger;
      MyInteger(void);
      MyInteger(const MyInteger&amp;);
      MyInteger(const __int64&amp;);
      ~MyInteger(void);
  
      static MyInteger const minValue;
      static MyInteger const maxValue;
  
      MyInteger&amp; operator = (const MyInteger&amp;);
      MyInteger operator + (const MyInteger&amp;) const;
      MyInteger operator - (const MyInteger&amp;) const;
      MyInteger operator * (const MyInteger&amp;) const;
      MyInteger operator / (const MyInteger&amp;) const;
      MyInteger&amp; operator += (const MyInteger&amp;);
      MyInteger&amp; operator -= (const MyInteger&amp;);
      MyInteger&amp; operator *= (const MyInteger&amp;);
      MyInteger&amp; operator /= (const MyInteger&amp;);
      MyInteger operator % (const MyInteger&amp;) const;
      MyInteger&amp; operator %= (const MyInteger&amp;);
      MyInteger&amp; operator ++ ();
      MyInteger operator ++ (int);
      MyInteger&amp; operator -- ();
      MyInteger operator -- (int);
      bool operator == (const MyInteger&amp;) const;
      bool operator != (const MyInteger&amp;) const;
      bool operator &gt; (const MyInteger&amp;) const;
      bool operator &lt; (const MyInteger&amp;) const;
      bool operator &gt;= (const MyInteger&amp;) const;
      bool operator &lt;= (const MyInteger&amp;) const;
  
      int toStdInt() const
      {
          return (int)numero;
      }
      float toStdFloat() const;
      double toStdDouble() const;
      char toStdChar() const;
      short toStdShortInt() const;
      long toStdLong() const;
      long long toStdLongLong() const;
      unsigned int toStdUInt() const;
      __int64 toStdInt64() const;
      unsigned __int64 toStdUInt64() const;
      unsigned long long int toStdULongLong() const;
      long double toStdULongDouble() const;
  
      template&lt;class Type&gt;
      Type&amp; operator[](Type* sz)
      {
          return sz[toStdULongLong()];
      }
  };
  
  template&lt;class Type&gt;
  Type* operator+(const Type* o1, const MyInteger&amp; o2)
  {
      return ((o1) + (o2.toStdInt()));
  }
  </code></pre>
  
  <p>I'd like to use this class to access array elements like this:</p>
  
  <pre><code>MyInteger myInt(1);
  int* intPtr = (int*)malloc(sizeof(int) * N);
  intPtr[myInt] = 1;
  </code></pre>
  
  <p>I thought that the function</p>
  
  <pre><code>template&lt;class Type&gt;
  Type* operator+(const Type* o1, const MyInteger&amp; o2)
  {
      return ((o1) + (o2.toStdInt()));
  }
  </code></pre>
  
  <p>could solve my problem, because as this post reports (<a href="https://stackoverflow.com/questions/8111357/type-of-array-index-in-c">Type of array index in C++</a>) "The expression E1[E2] is identical (by definition) to *((E1)+(E2))", but I get the C2677 error ('[' operator: no global operator found which takes type 'MyInteger' (or there is no acceptable conversion))</p>
  
  <p>Can someone clarify me this situation?
  Thanks</p>