Static
Static Variables
Scope: Limited to the block/function where it’s declared
Lifetime: Entire program (exists from start to end)
Value persists across function calls
Use cases:
Counting function calls
Caching values
Maintaining state without global variables
Static Data Members
Only one copy exists for the entire class
Shared by all objects
Not tied to any specific instance, can be accessed directly using class name.
Lifetime: Exists for the entire program (Created once, destroyed at program end)
Static Member Functions
Static members functions are functions of a class, they can be called using class name, without creating object of a class.
They can access only static data members of a class, they cannot access non-static members of a class.
Belong to the class, not to objects (Hence, No
thispointer)Can be called without creating an object
Cannot access non-static members
Can only access:
Static data members
Other static functions