Skip to main content
GameDev.net gamedev.net
🔒 Locked

c++ [] operator with multiple parameters

Started by cignox1 Aug 14, 2007 at 2:46 AM 3 replies 1k views
Original Post
cignox1
cignox1
Hi all, I've just made a typo that AFAIK should have been catched by the compiler but it wasn't. I tried to access to a float[][] by using values[2,1] instead than values[2][1]. The program didn't worked as expected, of course, but no errors were reported by the compiler of the linker (Visual c++ 2005). What does this operator use mean? Thank you!
Barius
Barius
comma operator.

values[2,1] == values[(2,1)] == values[1]
ToohrVyk
ToohrVyk
It's not operator[], but operator,, which is used here in a manner which is equivalent to:
int temp = (2,1);values[temp]


Where a,b, in C and C++, first evaluates a, and then evaluates and is equal to the value of b. In your case, since a is 2 and thus has no side-effect, your code is in fact accessing values[1].
Trenki
Trenki
The comma (,) is an operator by itself (which you can overload as well). The default operation of the comma operator is to return its second argument. So the access to the array by array[2,1] will most likely be equivalent to array[1].
AFAIK the [] operator can only take one argument, but I may be wrong with this.

Edit: Damn it! Two were faster than me
cignox1
cignox1
Me stupid! I always repeat to myself to never stay away from c++ for more than a couple month :-)
Yep, and most probably this is not even the only thing I forgot (templates syntax being another one :-(

Thank you!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.