How to change the meaning of pointer access operator
        Posted  
        
            by kumar_m_kiran
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by kumar_m_kiran
        
        
        
        Published on 2010-05-24T14:07:39Z
        Indexed on 
            2010/05/24
            14:11 UTC
        
        
        Read the original article
        Hit count: 279
        
Hi All,
This may be very obvious question, pardon me if so.  
I have below code snippet out of my project,
#include <stdio.h>
class X
{
  public:
   int i;
   X() : i(0) {};
};
int main(int argc,char *arv[])
{
  X *ptr = new X[10];
  unsigned index = 5;
  cout<<ptr[index].i<<endl;
  return 0;
}
Question
  Can I change the meaning of the ptr[index] ? Because I need to return the value of ptr[a[index]] where a is an array for subindexing. I do not want to modify existing source code. Any new function added which can change the behavior is needed.
Since the access to index operator is in too many places (536 to be precise) in my code, and has complex formulas inside the index subscript operator, I am not inclined to change the code in many locations.
PS :
1. I tried operator overload and came to conclusion that it is not possible.
2. Also p[i] will be transformed into *(p+i). I cannot redefine the basic operator '+'.
So just want to reconfirm my understanding and if there are any possible short-cuts to achieve.
Else I need fix it by royal method of changing every line of code :) .
© Stack Overflow or respective owner