I thought I'd try beefing up my C++ and OpenGL by looking at the recently-released Doom 3 source. Much learned so far, but I've hit a wall. The class detailed here has methods
float operator[] (int index) const
and
float & operator[] (int index)
whose bodies both read
return ( &x )[ index ];
where x
is one of the class' two data members (the other being y
; this class is for 2-vectors).
While I can understand the syntax of each version's header/prototype, I don't get why they're both present.
const
seems to appear (or not appear, as preferred) only to distinguish the headers sufficiently to allow compilation. (That is, remove const
and VS2010 refuses to compile, similarly if both headers end in const
.)
And why return a ref to a float? None of the class' seven other float-type methods do this, so I'm guessing efficiency isn't a factor (tho' maybe this operator's called vastly more often than the others).
Appreciate any insight as to what's going on here...