0

If I define a static variable in classA:

static int m_val; 

and initialize like

int classA::m_val = 0; 

Can I use directly m_val as it is in order to access it in ClassA (or any other class) or should I use it like classA::m_val.

2
  • 1
    classA::m_val everywhere except within classA Commented Apr 9, 2014 at 16:28
  • Inside classA it is sufficient to write m_val. Commented Apr 9, 2014 at 16:30

1 Answer 1

4

Inside of ClassA, just write m_val. Outside of ClassA, ClassA::m_val.

However, m_val is not const in your example, so it (typically) should be private anyway. In that case, you'd not access it directly from other classes but provide a member function to retrieve a copy:

class ClassA { private: static int m_val; // ... public: static int GetVal(); }; 

Implementation:

int ClassA::m_val = 0; int ClassA::GetVal() { return m_val; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.