How to write C++ getters and Setters is part of Stack OverflowGetters & Setter.
I have heard that the Java style of writing setters and getters is what I should write it in.I was told it is incorrect.What does that mean?I don't know how to write the setters and getters.
The need for getters and/or Setters is justified.E.g.Maybe we write only the getter, or we do some checks in the setter.
There are two different forms of "properties" in the standard library.The system should interact with Foo.Neither is correct.
A reference to the underlying X member allows both sides of the call site to observe changes initiated by the other.The X member is visible to the outside world because it's identity is important.If X is assignable, there is more to the property than just the get side.
We return a copy of the X member and accept another copy.Changes on either side don't spread.In this case, we only care about the value of x.
I have come to believe that the whole notion of getter/setter is usually a mistake.The correct answer is usually a public variable.
The public variable needs to be of the correct type.We can either write a getter that does some checking of the value being written, or we can only write an effectively const object.
"X is an int." is what both of those are saying.It's not really an int, but something sort of like one, with these extra restrictions.
If a careful look at X shows that it is a different type, then create it as a public member of that type.The bare bones could look like this.
The user can specify a function that assures the value is correct, or it can modify it or throw it.
To get an integer type that only allows values from 0 to 10 and saturates at 0 and 10, we might write code on this general order.
We can do more things with a foo if we know that it will always be in the range.
With this, we can make the member public because the type we've defined it to be is the kind we want, not something tacked on after the fact.
It's possible that we want to restrict the values in some way.If we only want a type that's effectively read-only, we can use a template that defines a constructor and operator T, but not an assignment operator that takes a T as its parameters.
Some cases of restricted input can be more complex.For example, if you want a relationship between two things, the bar must be between 2x and 3x foo.There are two ways to deal with that.One can use the same template, but with the underlying type being a std::tupleint, int>, and go from there.If your relationships are complex, you may want to create a separate class to define the objects in that relationship.
Define your member to be of the type you really want, and all the useful things the getter/setter could/would do get subsumed into the properties of that type.
The first issue with your version is that you should not pass values.This avoids copying.It is possible that the value can be moved, but not always.For basic data types.It's ok to use values instead of references.
There is a problem with the above solution.The object should be marked const since get_x does not modify it.This is part of a principle called const correctness.
The get_x method cannot be called on a const object.It is illegal to call an object a const object because a non-const method can modify it.
The above variant is correct.There is another way of writting it that is less Java ish.
I prefer the new trailing return function style.It is possible to say that (e.g.I write auto foo() instead of int.
We can overload on && and return an r value reference to x_ for performance reasons.
Many thanks to the people who commented on the post and to StorryTeller for his suggestions on how to improve it.
The main error is that if you don't use references in the parameters and return value, you may risk performing unneeded copies in both get/set operations.
This will keep the const correctness, which is a very important feature of C++, and it's compatibile with older versions.
If you do something wrong the compiler will help you sort it out, but I find the use of get/set superfluous.