Tuesday, August 29, 2006

Initialize static, constant member variables

Initialize in this way

class CMine
{
static int m_nCount;
public:
static int count() { return m_nCount; }
};
int CMine::m_nCount = 0;

only static const int m_nCount = 0; can work inside the class. Arrays, Non-integral and non-const statics must be initialized externally.

--
initialize no-static const variable : use initialize list.
initialize reference variable: use initialize list.

const int m;
int &r;

CMine(int n): m(1), r(n) {};

No comments:

Post a Comment