Basic Class Question

Is it always best to make a set and get function for each private data member that will be manipulated in the class? Appreciate all thoughts about this.
- DangerD
If you need that private member to be accessed by many other functions, you should have get/set functions. If that member is used only within the class and a few external functions you shouldn't
You could also declare the function to be a friend of the class.
"If you need that private member to be accessed by many other functions, you should have get/set functions. If that member is used only within the class and a few external functions you shouldn't."

What about "a few external functions"? How could a external function access the private member?
As I said, declare the function as a friend to give it access to private members.
Overuse/widespread use of setters and getters are a tell-tale sign of a problematic design.

Every data member that has a setter and getter method is essentially not encapsulated at
all; it implies that the user of the class is smart enough to manipulate the value and the
class isn't. One could argue therefore that the data member is better placed somewhere
else.


Topic archived. No new replies allowed.