Author Topic: & Vector::operator()  (Read 15887 times)

youcan

  • Newbie
  • *
  • Posts: 6
    • View Profile
& Vector::operator()
« 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.

fmk

  • Administrator
  • Full Member
  • *****
  • Posts: 233
    • View Profile
Re: & Vector::operator()
« Reply #1 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);


youcan

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: & Vector::operator()
« Reply #2 on: August 20, 2020, 02:50:26 AM »
Thanks. That clearly explains it.