Quote:Original post by Conner McCloud
In short: don't do it.
In long: Firstly, get/set pairs strongly suggest you're doing something wrong. You've also made your code unneccessarily complex. There's a completely superfluous level of indirection that you now have to keep track of, with the only benefit being you've saved yourself from writing two whole lines of code. Additionally, by definition all SSGEntity<T> members have no additional functionality beyond just setting and retrieving values directly. So they are a public variable that is hard to read. You've limited yourself to a single interface (T get() and void set(T)), which will make more complex cases more difficult to write than they should be, completely negating the benefit of not writing those simple cases. And its ugly.
CM
If you provide both get and set methods, just make the variable public, or at least make only one function T& get()
The rules of encapsulation say you shouldn't have any public member variables. People try to adhere to this by making get/set methods. Both are missing the point.
I say, encapsulation is where the user of code only needs to know the interface, not the internals. So in my reading, there isn't any problem
at all with public member variables, because where does it say "interface" means "function"?
The benefit of making those variables private is far outweighed by the benefit of having clear and consise code. The
only reason get/set methods might be acceptable are when there are extra steps that need to be taken (like locking a mutex) But even that is a weak argument for get/set functions.