Skip to content

book1 chapter3, vec3, what is the difference of "double operator[](int i) const {return e[i];}" and "double& operator[](int i) {return e[i];}" ? #1477

Answered by hollasch
jia-heng asked this question in Q&A
Discussion options

You must be logged in to vote

I'm afraid this is some C++ low-level detail that's unfortunately necessary here. We really try to keep C++ specifics to a minimum. This could look quite different in your own code, depending on your language of choice.

To be a bit more concrete, the first form lets you grab elements out of any vec3 object, like

vec3 v = foo();
double x = v[0];  // Deconstruct the vector v
double y = v[1];
double z = v[2];

The second form lets you change/set vector values, like so:

vec3 v;
v[0] = 0.0;
v[1] = 10.0;
v[2] = 100.0;

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@hollasch
Comment options

Answer selected by jia-heng
@jia-heng
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants