A brief analysis of C + + Get class member function address method

Source: Internet
Author: User

A function can be called directly in the C language using the function address:
void print () {   printf ("function print");} typdef void (*fun) (); Fun F = print;f ();
class non-static member functions in C + + must be called through an instance, and class member functions in C + + are called:
Class Test{public:void print () {   printf ("function print");}};
we can also invoke the following by defining a function pointer:
typedef void (test::* Fun) (), Fun f = &test::p rint;test t; (t.*f) ();
if we want to take the function address, direct strong transfer Error
unsigned int a = reinterpret_cast<unsigned int> (f);//Compilation error

http://bbs.pediy.com/showthread.php?t=47835

Here's the answer.

char buf [+] = {0};sprintf (buf, "%u", f); int a = Atoi (BUF);
In fact, we know that the class member function is just the first parameter of the function is the this pointer, but the this pointer is not pushed through the normal function parameters, but put into the ecx.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A brief analysis of C + + Get class member function address method

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.