Update static field in member method : static « Class « C++






Update static field in member method

  
#include <iostream>
using namespace std;
class SomeClass 
{
  public:  
    SomeClass(int value) { some_value = value; };
    void show_data(void) { cout << data << ' ' << some_value << endl; };
    static void set_data(int value) { data = value; };
  private:
    static int data;
    int some_value;
};

int SomeClass::data;

int main(void)
{
   SomeClass my_class(1001);
   my_class.set_data(5005);
   my_class.show_data();
}
  
    
  








Related examples in the same category

1.Static member variables and functions
2.static functions and ID numbers for objects
3.Using a static data member in a class
4.Static member data.
5.Accessing static members without an object.
6.Accessing static members using non-static member functions.
7.Static member functions.
8.static members in classes
9.Calculate salary using static members.
10.static counter
11.Reference static method along with class name
12.static field is shared among instances