Yesterday, I used C ++ to encapsulate a game function that I wrote. One small problem is that two thread functions must be defined in the encapsulated C ++ class, for example:
1. class
2 .{
3. public:
4. ();
5 .~ A ();
6.
7. void XXX (void * p); // This function is a thread callback function.
8 .};
XXX is the thread callback function, which must be called by the _ beginthread () function in another thread startup function. But when I write this:
_ Beginthread (XXX, 0, NULL );
The compiler reports an error .. That is, compilation fails. I thought about it carefully and thought it was indeed impossible to compile it, because the thread function must know the function entry address during compilation, and I write it like this, the XXX portal address is indeed unknown ..
So I think of two methods to change the unknown to known. One is to define XXX as a static member function, as shown below:
Static void XXX (void * p );
The other is to define XXX as a global function.
In fact, it is to let _ beginthread () Know the entry address of this function. Global functions and static functions are stored in one place and are known during compilation.
When I thought everything was okay, I found another error in the XXX () function ..
It indicates that I have used a member that is not in the category. This is actually the meaning:
1. class
2 .{
3. public:
4. ();
5 .~ A ();
6.
7. static void XXX (void * p); // This function is a thread callback function www.2cto.com
8.
9. int a; // It will be modified in the XXX Function
10 .};
The member variable A of a will be modified in the XXX () function, but it cannot be seen in the XXX function.
You may ask, why can't you see it? This is what I usually write. For example, other member functions defined in A can directly access.
Indeed, other common member functions in Class A can access this member variable. However, it is easy to ignore that there is actually a hidden parameter in the general member function, which is the this pointer to this class. we access the member variable a in the member function, it is actually accessing this->. So our member function can find this a, but our thread function does not pass this parameter. XXX () is created using the _ beginthread () function, so if we want to pass a parameter to XXX (), we must write it in the third parameter of _ beginthread (). We can see that the third parameter I wrote above is NULL, therefore, member variable a cannot be accessed in XXX (), because this pointer is not available.
After understanding this, it will be very easy. In _ beginthread (), pass the this pointer to the XXX () function, and then in XXX () in the function, convert the void * type to the pointer of Class. As follows:
1. class
2 .{
3. public:
4. ();
5 .~ A ();
6.
7. static void XXX (void * p); // This function is a thread callback function.
8. void start (); // thread start Function
9. int a; // It will be modified in the XXX Function
10 .};
11.
12. void start ()
13 .{
14. _ beginthread (XXX, 0, this );
15 .}
16.
17. void XXX (void * p)
18 .{
19. A * handle = (A *) p;
20. int B = handle->;
21 .}
Am I a good guy... Haha. Okay, I am flooded.
From cainiao surfaced