In C++, is there a way to override the array subscript using a macro? For example, I would like to use something like this:
#define operator[](index) GetAt(index, __FUNCTION__)
Using this macro, whenever I use an array subscript, I would be able to know the name of the calling method.
For example:
class A
{
....
int myArray[10];
int& operator[](int index)
{
return myArray[index];
}
int& GetAt(index, const char * caller)
{
printf("Called by %s", caller);
return myArray[index];
}
....
}
class B
{
void doSomething()
{
A obj = new A();
int val = obj[1]; //<- this will call GetAt(..) and printf the caller's name.
}
}
#define operator[](index) GetAt(index, __FUNCTION__)
Obviously the #define does not work, but I'm looking to do something similar.