View Single Post
Old 10-31-2004, 01:25 PM   #1 (permalink)
Rekna
Junkie
 
[C++] overloading [] on pointers

I have a vector and matrix class and i'm impementing an iterative method to solve a IBVP. In this class I have overloaded the [] to work on the class just fine.

However, now I have ran into a problem where I don't want to be passing around whole objects and copy them around I want to do it with pointers to the objects. However I still want to be able to use my array notation.


So currently I use syntax like the following:

Vector X(16);
Vector *X0=&X;

...

(*X0)[0]=.....

I'd like to be able to just use

X0[0]=....


So I tried adding a non-member function like the following

double& operator[] (Vector* V, unsigned i)
{
return (*V)[i];
}

however the compiler gives me an error saying [] must be a non static member.

I can't see how to overload this operator as a static member. Anyone have any suggestions?
Rekna is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73