SimCenter Training > Programming Bootcamp

& Vector::operator()

(1/1)

youcan:
In the last Vector exercise, what is the difference between the function Vector::operator() and the function &Vector::operator()? They all return data[ x ]. Does the latter mean it will only return the value by reference but not address? And how can I choose to call the latter function instead of the former one in the main function? Thanks.

fmk:
the two operators are for the two possible uses, lhs of an equation and rhs of an equation:

the & is for the lhs case, where we want to return a reference so that we can change the address:
    v(1) = 1.0;

the non & that ends with const is for the rhs (the const at the end means we are not changing the vector in the method)
    double a = v(1);

youcan:
Thanks. That clearly explains it.

Navigation

[0] Message Index

Go to full version