Accessing Static Class Members

Accessing Static Class Members

Because static class members are not associated with a particular object of their class, you can access them even if you haven’t instantiated any object. In the following example, the static member function read_val() returns the value of the static member n, although no instance of class C exists:

 class C{private: static int n;public: static int read_val() {return n;}};int main(){ int m = C::read_val(); // no object of type C exists; OK}


Share the Post: