问题
I write code like
template <typename XAxis, typename YAxis>
class Interpolation {
public:
using x_value_type = typename XAxis::value_type;
using y_value_type = typename YAxis::value_type;
Interpolation(const XAxis& x_axis, const YAxis& y_axis)
: _x_axis(x_axis), _y_axis(_y_axis)
{}
public:
y_value_type interpolate(const x_value_type& x) const;
private:
const XAxis _x_axis;
const XAxis _y_axis;
};
when i want to intent that member variables is immutable.
I feel a little anxious why I think that I might be known above code is illegal or bad code by some reason.
How do you think that constant member variables are beneficial or not?
Please tell me with your reason.
回答1:
A practical advantage of const
members is that unless they're of a class with user defined default constructor, they have to be explicitly initialized. So a ¹standard-conforming compiler will catch any missing initialization. It's also an advantage that const
members express the intended functionality, namely no change over the lifetime of an object, and so help the compiler to detect any code that otherwise would (erroneously) change the values.
On the other hand, which can more important, with the default functionality they prevent copy assignment and moving.
These considerations also apply to reference data members, which can be regarded (conceptually) as auto-dereferenced const
pointer members.
For client code, but not the class' own code, the logical const
-ness, the immutability, can be expressed by using non-public
non-const
data members with const
accessor member functions.
¹ Unfortunately Visual C++ 2015 accepts at least some cases of uninitialized const
member that doesn't have a user-defined default constructor, so as of this writing one can't be 100% sure that the compiler will catch missing initializations.
回答2:
To me it seems unnecessary as you can always have const
member functions to make sure you don't modify the fields. Also, having const
member variables also disable move semantics which means an object of that type must always be copied and can't be moved from. I would refrain from doing it.
回答3:
const
member variables do have some minor benefit. Because the compiler knows that their const
, and their value cannot change, the compiler might be able to better optimize any code and expressions that reference the const
class members.
A more practical benefit is that any logical code errors, that attempt to modify, these class members will now fail to compile. Otherwise, the logic error may not be immediately apparent, and result in burning up a lot of time later, tracking it down.
回答4:
Const members:
prevent copy and move assignment
are known never to change
make your object truly immutable, which has the following advantages:
-
offers performance advantages when sharing data in a multi-threaded program.
allows the compiler to be very aggressive with redundant load optimisations
You could achieve the same with const accessors to mutable data.
That's true, at the expense of more (redundant) code to maintain.
来源:https://stackoverflow.com/questions/38155699/are-there-advantages-of-using-const-member-variable-in-c