It depends on what you want to do. If you want client code to be able to modify your private xval_ from outside the class, you want to return double&
; however, you probably don't want to do that.
Your other two choices are to return a double
or a const double
. In this case, they're both the same; you don't get any benefit from adding a const
in this case, so you should simply return double
because the const
is redundant.
double x() const;
If you had pointers or references, then you might want to consider specifying the const
for the return type depending on what you wanted to do. For example:
const double* xPtr() const;
double* const xPtr() const;
const double* const xPtr() const;
Note I did not specify inline
anywhere. That's a separate discussion that you didn't ask about in your original post. For this example, you can go either way--include it or omit it--the inline
keyword has nothing to do with your original question.