Question

I am still not quite sure about the "move semantics". In a simple point class,

class point
{

private:

    double xval_, yval_, zval_;


public:
    // Constructs
    // ...


public:

    // Access (return xval_)
    inline const double x() const;

    // Edit
    inline double& x();
};

Should I use inline const double x() const; or inline double x() const; in my Access function?

Was it helpful?

Solution

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.

OTHER TIPS

I do not see a sense to return const double from the function,

There could be a sense to define the function as constexpr provided that you need to use its result at compile time.

In other cases the return type should be defined as simply double.

When returning by value, it's usually best not to return a const. Reason being: as long as your consumer doesn't change your state, you really don't care what it does with it. Also, (excluding const cast) the client can always bind to a non-const by copying - but why would you want to force it to do that?

It's good to define const access functions (if you're able), because it allows a user of the class with a const reference to access the value (without having to copy away the const reference).

For the function above, the form:

inline const double x();

doesn't really make much sense when returning a non-pointer or reference type. Typically you reserve the const modifier when returning a pointer or a reference.

As I said in my comment, for built-in types, I don't think it matters. See Impact of returning const value types in C++11 on move semantics for why:

An expression calling a function that returns by value is a prvalue. However, there are no const prvalues of non-class non-array type (§5/6):

If a prvalue initially has the type “cv T,” where T is a cv-unqualified non-class, non-array type, the type of the expression is adjusted to T prior to any further analysis.

This means that there's no difference between your two definitions of the function. Whether it returns a const int or just an int is irrelevant because the expression is never const.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top