Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 10-31-2004, 01:25 PM   #1 (permalink)
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  
Old 10-31-2004, 11:30 PM   #2 (permalink)
Junkie
 
Location: San Francisco
Just use references if you can -
Vector & X0 = X;
n0nsensical is offline  
Old 11-01-2004, 10:35 PM   #3 (permalink)
Junkie
 
Thanks that worked great. I never actually thought about creating refrences like that.

If I have X0 and X1 which are refrences to 2 different vectors swap(X0,X1) will only swap the refrence right? I want to make sure it behaves like a full deep copy without actually performing a deep copy.

Also does anyone know if there is a way to overload [] operators not in a class? I think it would be really cool to make a class that behaved exactly like an array but with different symantics. Like array bounds checking, general bounds checking, maybe 1 based arrays instead of 2 based. It could be useful to develop using a class like this and then for a release move to the normal arrays.
Rekna is offline  
Old 11-02-2004, 06:57 PM   #4 (permalink)
Upright
 
You can only overload [] by member functions of a class. Same goes for = () ->

Not sure if this is what you mean WRT a class behaving like an array with different semantics...Use the STL containers.

Use iterators to navigate over STL containers like the vector, and for element access use at(). You can try/catch an out of range error on the at() command as it does a range check. If you want you can still use the operator[], but you have to make sure you perform the range check. Iterators have the begin()/end() functions that should be used.
__________________
It's not that I don't care, it's just that I don't care enough!
deekaybee is offline  
 

Tags
overloading, pointers


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 09:08 PM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

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