My Opinion on pointing wndproc to a class member function

Source: Internet
Author: User

I used to think about how to call windows APIs such as wndclass and createthread in the class and how to use the member function address when passing in the function parameter address. For research, I wrote an example code:

#include <iostream>#include <stdio.h>using namespace std;typedef int (__stdcall *XSSH_SAY_HELLO)(int);class XTestMethod{typedef int (__stdcall XTestMethod::*XSayHello)(int);public:XSayHello say;int hi;public:XTestMethod();virtual ~XTestMethod();int sayhello(int arg);};XTestMethod::XTestMethod(){say=&XTestMethod::sayhello;hi=1000;}XTestMethod::~XTestMethod(){}int XTestMethod::sayhello(int arg){printf("\r\nsay:%d%d\r\n",arg,this->hi);}int main(int argc, char *argv[]){XTestMethod t;XSSH_SAY_HELLO call=*(XSSH_SAY_HELLO*)&t.say;call(0);system("pause");return 0;}

During debugging, it is found that the this pointer in the stack is empty before calling the class members. After the input, the value of the output result is random, and the latter directly leads to an error (empty reference ).

For member functions and member variables, the offset addresses of the same member variables of different objects of the same type are the same. When accessing member variables, their addresses are calculated based on the overlapping object addresses, if you use the preceding method to access the member function and then ask the Member variable, an error occurs. Even if the object pointer is pushed into the stack before accessing the member variable, we should not only consider the address of the object to be pushed into, but also use the member function as a global function, it is no different from the purpose of using static member functions and loses the meaning of the facial object.

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.