I am learning today about static class functions in C++ and I can’t really understand what are they good for? Does anyone have some good examples where they can be applied successfully?
Thanks, Boda Cydo.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Static member functions of a class are just ordinary (non-member) functions. You do know what ordinary functions are good for, do you? So, static member functions are good for the same things for the same reasons.
It is just that sometimes an ordinary function has a tight relationship with the class, so it makes sense to declare it as a static member of the class, instead of declaring it as a completely independent standalone function. It helps you to express the fact that the function has that tight relationship with the class. Besides, this gives that function full access rights to the innards of the class – to its private and protected members.
The latter actually makes it possible to implement some useful programming idioms and patterns using static member function. Do a search, for example, for “static constructor” idiom.