I am having trouble understanding the difference between Array obj;
and Array* obj = new Array;
while overloading the array index operator []
. When I have a pointer to the object, I get these error messages on VS 2010.
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
could be 'Array &Array::operator =(const Array &)' while trying to match the argument list '(Array, int)'
#include <iostream>
class Array
{
int arr[10] ;
public:
int& operator[]( int index )
{
return arr[index] ;
}
};
int main()
{
//Array* obj = new Array; Error
Array obj; // Correct
for( int i=0; i<10; ++i )
obj[i] = i;
getchar();
return 0;
}
Can some one explain the rationale between the two kind of instances for operator overloading? Thanks.