Fork me on GitHub

Project Notes

static

About the static keyword in C++ - static variables, static objects, static member variables, static member functions.

Notes

Static Variables inside Functions

Static variables when used inside function are initialized only once, and then they hold there value even through function calls.

void counter() {
  static int variable = 0;
  cout << "variable is now : " << ++variable << endl;
}

Static Class Objects

Static keyword works in the same way for class objects too. Objects declared static are allocated storage in static storage area, and have scope till the end of program.

void static_object_id() {
  static StaticDemo static_object;
  cout << "static_object id is still : " << static_object.getId() << endl;
}

Static Class Variables

Static class variables have a single storage and are shared by all object instances of the class. The static class variable must be initialised/allocated at some point outside of the class definition. If the sttic is also const, then it can be initialised inline in the class definition

class Test {
private:
  int id;
  static int counter;

public
  static int const MAX = 42;

public:
  Test() {
    id = counter++;
  }
  int getId() {
    return id;
  }
};

int Test::counter = 0;

Static Class Methods

These functions work for the class as whole rather than for a particular object of a class. They can’t access instance variables, but can access static members.

class Test {
private:
  static int counter;

public:
  Test() {}
  static int getCounter() {
    return counter;
  }
};

int Test::counter = 0;

Test::getCounter();

Running the Example

See example.cpp for details. A makefile compiles and runs:

$ make
c++ -std=c++17 -g -Wall -O3    example.cpp   -o example
./example;
incr_static_variable_in_function() => static_variable_in_function is now : 1
incr_static_variable_in_function() => static_variable_in_function is now : 2
incr_static_variable_in_function() => static_variable_in_function is now : 3
static_object_id() =>
  id initialised from static_class_variable: 100
  static_class_variable is now : 101
object_1 =>
  id initialised from static_class_variable: 101
  static_class_variable is now : 103
object_2 =>
  id initialised from static_class_variable: 102
  static_class_variable is now : 103
StaticDemo::staticClassMethod() =>
  StaticDemo::STATIC_CLASS_CONST    : 100
  StaticDemo::static_class_variable : 103
static_object_id() =>
  id initialised from static_class_variable: 100
  static_class_variable is now : 103

Credits and References

About LCK#178 C++
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.