@ wrote... (9 years ago)

A static variable in a class method are shared between all instances of that class and not per class instance.

#include <stdio.h>

class Foo {
public:

    int foo() {
        static int i = 0;
        i++;
        return i;
    }
};

int main() {

    Foo a, b;

    printf( "a: %d\n", a.foo() );
    printf( "b: %d\n", b.foo() );
}
a: 1
b: 2
Category: tech, Tags: cpp, programming
Comments: 0
Click here to add a comment