Sunday, 29 March 2015

Static member objects in C++

Static member objects in Cpp with code example:

Like static members and Static member functions it is possible to create the static member objects.

See the below example:

class TestStaticObj
static TestStaticObj ocObjStatic;
int m_iVal;
TestStaticObj(int iVal) : m_iVal(iVal) {}
TestStaticObj(const TestStaticObj&); 
  static TestStaticObj* GetInstance() { return &ocObjStatic; }
  int m_iData () const { return m_iVal; }
TestStaticObj TestStaticObj::ocObjStatic(22);  // initialize static object
int main ()
  cout << TestStaticObj::GetInstance()->m_iData() << endl;
  getchar ();
  return 0;

See the below link to understand the implementation of the Static member objects.

No comments: