Method for calling a class member function

Source: Internet
Author: User

It is very troublesome to compile callback functions using MFC or other C ++ applications. The root cause is that the callback function is based on the C-programming windows SDK technology, not for C ++,ProgramA member can directly use a C function as a callback function. However, if you attempt to directly use a C ++ member function as a callback function, an error may occur, or even compilation may fail. The error is that a common C ++ member function implies a passing function as a parameter, that is, the "This" pointer, c ++ transfers a pointer to itself to its member functions so that program functions can access data members of C ++. This can also understand why multiple instances of the C ++ class can share member functions but have different data members. Because of the function of this pointer, when a callback-type member function is installed as a callback function, the number of function parameters does not match due to the implicit this pointer, this causes the callback function to fail to be installed.

The key to solving this problem is to prevent this pointer from working. There are two methods:

1. Without a member function, you can directly use a common C function. To enable member variables of the member class in the C function, you can use the friend operator, in C ++, you can describe the C function as a friend of the class.

2. Use a static member function. The static member function does not use the this pointer as an implicit parameter, so that it can be used as a callback function.

Static member functions have two major features:

First, it can be used without a class instance;

Second, you can only access static member variables and static member functions, but not non-static member variables and non-static member functions.

In C ++, the purpose of using a class member function as a callback function is to access all member variables and member functions. If this function is not implemented, it will not be of practical significance. The solution is also very simple, that is, to use a static class pointer as a class member, by initializingStatic pointerFor example, m_pthis = this. Then, you can use this static pointer in the callback function to access all member variables and member functions.This method is applicable when only one class instance exists.Because multiple class instances share static class members and static member functions, this causes the static pointer to point to the last class instance created. To avoid this situation, you can use a callback parameter to pass the this pointer, so as to share data members. This method is a little troublesome and I will not go into details here.

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.