SimCenter Forum

SimCenter Training => Programming Bootcamp => Topic started by: youcan on August 19, 2020, 01:44:46 AM

Title: & Vector::operator()
Post by: youcan on August 19, 2020, 01:44:46 AM
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.
Title: Re: & Vector::operator()
Post by: fmk on August 19, 2020, 04:32:52 PM
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);

Title: Re: & Vector::operator()
Post by: youcan on August 20, 2020, 02:50:26 AM
Thanks. That clearly explains it.