Skip to content
Justice Tech School

static Members and this

static members belong to the class, shared by all objects. this is a pointer to the current object and enables chaining.

Example
#include <iostream>
using namespace std;
class Counter {
    static int total;
    int v = 0;
public:
    Counter() { total++; }
    Counter &add(int n) { v += n; return *this; }
    int value() const { return v; }
    static int count() { return total; }
};
int Counter::total = 0;
int main() {
    Counter a, b;
    cout << a.add(2).add(3).value() << " " << Counter::count() << "\n";
}

Result

5 2

Exercise

Pointer to the current object.

return *;

Quiz Test

What does static mean for a member?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.