How do static functions of a thread invoke non-static member functions or member variables of a class?

Source: Internet
Author: User

In multi-threaded programs, whether we use afxbeginthread, createthread, and _ beginthread to construct a thread function, because the thread function can only be a global function or a static function, the static function is used as an example below, static functions can directly access static members, but the non-static functions or member variables of the functions class must be used as an adapter. The Code is as follows:

Class cstaticthread
{
Public:
Static int threadfunc (lpvoid pparam );/
Protected:
Int threadfunckernal ();
}

Void cstaticthread: func ()
{
Afxbeginthread (treadfunc, this );
}

Int cstaticthread: threadfunc (lpvoid pparam)
{
Cstaticthread * pobj = (cstaticthread *) pparam;
Return pobj-> threadfunckernal ();

}

// Thread function kernal
Int cstaticthread: threadfunckernal ()
{
While (1)
{
// My thread
// To control class non-static member
}
Return 0;
}

The above example is used in our actual application. The following describes the differences between static member functions and non-static members.

 

Static member functions and member variables are mainly used for class sharing all objects.Declared static class members or member functions can be shared within the scope of the class.We call such a member as a static member and a static member function.

Static member initialization should be performed before the main function call and cannot appear in the class declaration.,The use of static members should be the class name and domain differentiation plus the member name, Although we can still use object names with some operation symbols and member names, but not recommended,Static class members belong to a class rather than a specific object.The difference between a static member function and a common member function lies in the lack of this pointer.

 

Static member functions:

It has two main functions:
1. Manage static data members;
2. Provides the function of Class scope, that is, the function implemented by objects is not required.

 

Because a static member function of a class has only one copy, the function used to access the data of the object is restricted. The static member function can directly access the static data members of the class. WhileTo access non-static data members, you must obtain the object name by passing parameters, and then access it through the object name.. The example of the thread above is to get the object name through parameter passing and then access it through the object. In this way, not only public members, but also private members and member functions can be accessed in the thread;

 

Note:

1) a static member can be inherited. The base class and the derived class share the static member;

2) constructors cannot be declared as static;

Constructor initializes each object, while static functions share objects in the class.

3) virtual functions cannot be static functions;

Why must virtual functions be non-static member functions?
If it is defined as a virtual function, it is dynamically bound, that is, it can be overwritten in the derived class, which is in conflict with the definition of static member functions.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.