liveBook · Manning

Accessor method

As explained in 100 C++ Mistakes and How to Avoid Them

An accessor is a class member function that provides access to an object's state without defining the object's state-changing operation.

An accessor is a member function that provides access to an object’s state without being its state-changing operation. A mutator, by contrast, changes state and must preserve the class invariant. An accessor may return a stored value or compute a result from stored state. section 7.2.2 section 8.2

Writing an accessor for every private variable can expose implementation details and burden clients with knowledge they should not need. Clients may come to depend on those details, making later changes harder. Provide access where it serves the class’s intended use, rather than automatically exposing every field. section 8.2

Listing 8.1 Class with computed values and trivial accessors
class Circle {
private:
    const static double PI;
    double radius;
    double area;
public:
    Circle() : radius(1), area(PI) {}
    Circle(double radius) : radius(radius), 
        area(radius*radius*PI) {}                        #1
    double getArea() const { return area; }              #2
    double getRadius() const { return radius; }
};
const double Circle::PI = 3.1415927;
 
int main() {
    Circle c1(3);
    std::cout << c1.getArea() << '\n';
    return 0;
}

This Circle stores both radius and a derived area, then returns the stored area through a trivial accessor. section 8.2

Listing 8.2 A class with a computing accessor
class Circle {
private:
    const static double PI;
    double radius;
public:
    Circle() : radius(1) {}
    Circle(double radius) : radius(radius) {}
    double getArea() const { return radius*radius*PI; }       #1
};
const double Circle::PI = 3.1415927;

The revised Circle computes area from radius when requested, eliminating the separate area variable. section 8.2

Accessors do not repair invalid object state. For example, returning a corrected age from an accessor does not make a stored negative age valid. Constructors must establish the invariant, and mutators and other state-changing methods must maintain it. section 7.2.2 section 7.3

A small set of accessors can also isolate representation changes. Date formatting and output functions can use accessors instead of directly accessing date fields, reducing the methods that need modification when the representation changes. The illustrative Date implementation is not recommended as a production date implementation. section 9.12

For inheritance, private base-class variables keep derived classes from changing state directly. Derived classes can use the base class’s accessors and validated mutators instead; protected variables can let them bypass invariant checks. section 8.4

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Contrasts trivial and computing accessors, then relates a minimal accessor set to representation changes.
Examples
Uses Circle area, Person age, and Date formatting to illustrate accessor choices and their consequences.
What you can do after
Evaluate accessor interfaces for unnecessary exposure and identify opportunities to compute values or reduce direct access to state.
How it connects
Connects accessors to encapsulation, class invariants, mutators, inheritance, and nonmember functions.

Also explained in

Compare how 13 books explain accessor method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.