Refer to static variable through its class name : static instance variable « Class « C++ Tutorial






#include <iostream> 
using namespace std; 
 
class MyClass { 
public: 
  static int num; 
  void display() { cout << num << endl; } 
}; 
 
int MyClass::num; // define num 
 
int main() 
{ 
  MyClass a, b; 
 
  // Set num through its class name. 
  MyClass::num = 100; 
 
  a.display();
  b.display();
 
  // Set num through an object. 
  a.num = 200; 
 
  a.display();
  b.display();
 
  return 0; 
}
100
100
200
200








9.25.static instance variable
9.25.1.Use a static instance variable
9.25.2.Refer to static variable through its class name
9.25.3.static data members