static

Methods without Object - static (1)

What we know now:

  • Methods are great

  • Name and variable ⟶ Method (like p.move(1,2))

  • ⟶ clear writing

But: global functions? Methods without an object?

  • Not bound to objects

  • Same scheme (“method of the class”)?

Example:

  • Adding points in C takes two const points and makes a third

  • Does not operate on any one parameter in particular

  • ⟶ global

point point_add(const point &l, const point &l);

Methods without Object - static (2)

Definition

Usage

class point
{
public:
    static point add(const point &l, const point &r)
    {
        return point(l.x()+r.x(), l.y()+r.y());
    }
};
point a(1,2), b(2, 3);
point c = point::add(a, b);