Sign in to follow this  
cignox1

c++ [] operator with multiple parameters

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this