Mega Code Archive

 
Categories / C++ Tutorial / Class
 

Share static variable among different class instances

#include <iostream> using namespace std; class MyClass {     static int a;     int b;   public:     void set(int i, int j) {       a=i;       b=j;     }     void show(); }; int MyClass::a; void MyClass::show(){   cout << "This is static a: " << a << endl;   cout << "This is non-static b: " << b << endl; } int main(void){   MyClass x, y;   x.set(1,1);   x.show();   y.set(2,2);   y.show();   x.show(); }