Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessors vs. public members

Tags:

c++

class

member

I have a class with a lot of built-in type members with read/write access. Should I make them public members and provide get/set methods for each one? How about structures?

like image 824
jackhab Avatar asked Mar 22 '09 12:03

jackhab


People also ask

What is the point of accessors?

What's the importance of accessor methods? In a nutshell, an accessor method controls access to its attribute by providing a single location to inspect and/or modify that attribute. Code can also be added to perform operations such as range checks.

What is a public accessor?

Accessors and mutators are public member functions in a class that get (accessors) and set (mutators) the values of class member functions. In other words, these are functions that exist solely to set or get the value of a class member variable.

Are getters and accessors the same?

For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively.

What are accessors?

What Does Accessor Mean? In computer programming, an accessor method is a method that fetches private data that is stored within an object. An accessor provides the means by which to obtain the state of an object from other program parts.


2 Answers

The whole reason to have accessors (getters) and modifiers (setters) is to provide yourself with an extra level of indirection.

This extra level of indirection allows you to provide a read only view of your variable to a public interface, while not allowing your data member to be changed. You could still use a private or protected setter.

Setters allow you to do special error checking, validation and corrections when a value is set. For example setDirectory(const std::string &strPath), you could make sure there is a terminating slash if the user didn't specify one. This ensures that your class state will always be valid.

Getters can also shield your members from having them exposed to allow pointers to them. By not allowing pointers to them from the outside, you can ensure that if your object goes out of scope it won't lead to a crash.

The extra level of indirection for getters/setters also allow you to be able to change the data member that they encapsulate.

With a getter you can also obtain different views of your data, example: getMinutes, when your data member is actually stored in seconds.

This is not the reason to use them, but a nice side effect of using getters and setters is that you can set a breakpoint inside your modifier for example to see exactly when it is changed.

Whether you should use them or not is a judgement call based on your need. If you have so many members that it is a huge pain to provide getters and settings you could consider storing the data members in a struct and using that struct inside your class instead. You could even provide getters/setters for an object for the whole struct at once.

like image 173
Brian R. Bondy Avatar answered Sep 29 '22 20:09

Brian R. Bondy


If there are invariants you need to preserve, then yes. Otherwise, don't bother.

like image 35
Nemanja Trifunovic Avatar answered Sep 29 '22 19:09

Nemanja Trifunovic