When we add a static data member in class, it will have only one copy per class.Let us understand with an example.//Header file#include <iostream>using namespace std;class base { static unsigned int m_classMember; //static data member unsigned int m_objectMember; //nonstatic data memberpublic: base() { m_objectMember = 0; ++ m_classMember; ++ m_objectMember; cout << “Count: “ << m_classMember << “ “ << m_objectMember << endl; }}; //Source file:unsigned int…
Category: Static Keyword
Static keyword Usage
Static function
Static function is used to access static data members of a class.Static function is called using class name and scope resolution operator. We do not need an object to call static function. //Header fileclass singleton { static singleton* m_instance; singleton() {} singleton& operator=(const singleton& ); singleton(const singleton& ); public: ~singleton() {} static singleton* getInstance(); }; //Source file singleton* singleton::m_instance = NULL;singleton* singleton::getInstance() { //static function accessing static data member m_instance if (NULL == m_instance) { return new singleton; …