Code
static void MyClass::ThreadEntryStatic()
{
//...
}
void MyClass::Begin()
{
CreateThread(..,ThreadEntryStatic,..);
}
In which condition we should use the static in class ?
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.
There are three places that the keyword static can be used. One is in the declaration of a struct/class when that context means that the method is a class method instead of an instance method. This means that this method is called directly and you don’t need an instance. From this static method, you can not access instance variables.
In MyClass.h
In MyClass.cpp
A second case where the static keyword is use is in the scope of a file where you don’t want the visibility of the variable declared to be visible outside the file. You can also use an anonymous namespace for this as well.
A third case where the static keyword is used is in the scope of a method and the value is retained between executions of the function (and initialized with the assignment the first time).