We can all agree on public variables being bad for encapsulation and all that. However, I noticed a lot of code that does this type of stuff:
class foo {
pri
I have to partially disagree both with @πάνταῥεῖ and @Rakete1111 's answers, considering how a class's definition is something that may evolve over time.
While it's true that, often, these getter methods are written by someone who's just heard the "no exposing members" mantra, they can also have legitimate uses:
vector works; when you call its operator[] you don't get a boolean&, you get some kind of proxy which, when assigned or assigned-to, does the appropriate bit extraction or setting.To sum up: The "dummy" non-const-reference getter can be a stub for other, meaningful, code.
That being said, it is often a good idea to just make the getter return a const reference or a value. Or just exposing the field in those cases where it's appropriate (and there are some of those too).