There's a recurring mantra, that getter/setter functions should be used to encapsulate your data. Hence many (unexperienced or coffee-overloaded) programmers get the idea they should use something like:
int& integer() { return integer_; }
but that isn't much different from simply writing:
class foo {
public: // <<<
int integer_;
string someString_;
// ...
};
Well, it adds a function call, but you cannot control what the client does with the reference.
If you really want to provide a getter function write:
const int& integer() const { return integer_; }
A corresponding setter function looks like:
void integer(const int& value) {
integer_ = value;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…