liveBook · Manning

Static method

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

A static member function is a C++ member function associated with a class rather than a particular object and therefore unable to access non-static instance state.

A static method belongs to the class rather than an object. It can use class-level state but cannot read instance values, so no object is required to call it. section 9.11

Mistake: leaving object-independent helpers as instance methods. In a Rational class, gcd and reduce appear to require an object even though their work can operate on supplied values, reducing readability. section 9.11

Prefer the form that matches what the method needs:

  • Declare gcd and reduce static when they use only parameters and class-level information. section 9.11
  • Keep a method non-static when it directly changes or reads the current object's state. section 9.11
  • Use a free function when placing the helper inside the class adds no clarity. section 9.11
Listing 9.28 Rational reworked to include two class methods
class Rational {
private:
    int num;
    int den;
    static int gcd(int a, int b) {               #1
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
    static void reduce(int& num, int& den) {     #2
        int div = gcd(num, den);
        num /= div;
        den /= div;
    }
public:
    Rational(int num, int den = 1) : num(num), den(den) { 
        reduce(this->num, this->den); }
    friend std::ostream& operator<<(std::ostream&, const Rational&);
    Rational& operator*=(const Rational&);
};
std::ostream& operator<<(std::ostream& out, const Rational& r) {
    out << r.num << '/' << r.den;
    return out;
}
Rational& Rational::operator*=(const Rational& o) {
    num *= o.num;
    den *= o.den;
    reduce(num, den);
    return *this;
}

int main() {
    Rational r1(3, 9);
    std::cout << r1 << '\n';
    return 0;
}

Compare the initial Rational implementation with a refactoring that makes gcd and reduce static and passes numerator and denominator explicitly. section 9.11

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

How deep it goes
Defines object and class access, then demonstrates a Rational refactoring without descending into language internals.
Examples
Uses Rational::gcd and reduce, including initial instance methods and a static-parameterized revision.
What it assumes
Assumes basic classes, instance variables, methods, and static variables while contrasting their access rules.
What you can do after
Apply static declarations to object-independent C++ helpers and choose free functions when they communicate intent better.
How it connects
Connects static methods with instance methods, static variables, helper methods, and free functions.

Also explained in

Compare how 5 books explain static 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.