Call class member function address directly

Source: Internet
Author: User

One, the use of member function pointers

In C + +, a pointer to a member function is a very special thing. For ordinary function pointers, it can be treated as an address and can be arbitrarily converted and invoked when needed. However, for member functions, regular type conversions are not compiled, and special syntax must be used when calling. C + + specifically prepares three operators for member pointers: "::*" for pointers, whereas "->*" and ". *" are used to call functions that the pointer points to. Like what:

class tt
  {
    public: void foo(int x){ printf("\n%d\n",x); }
  };
  typedef  void ( tt::* FUNCTYPE)(int );
  FUNCTYPE ptr = tt::foo; //给一个成员函数指针赋值.
  tt a;
  (a.*ptr)(5);  //调用成员函数指针.
  tt *b = new tt;
  (b->*ptr)(6); //调用成员函数指针.

Note the use of parentheses when calling function pointers, because. * and->* have a lower priority, they must be combined with the elements on both sides of the parentheses inside, otherwise not pass compilation. Not only that, but more importantly, you can't do any type conversion for the member function pointer, such as you want to save the address of a member function to an integer (that is, the address of the class member function), which is not possible by the normal type conversion method. The following code:

DWORD dwFooAddrPtr= 0;
  dwFooAddrPtr = (DWORD) &tt::foo; /* Error C2440 */
  dwFooAddrPtr = reinterpret_cast (&tt::foo); /* Error C2440 */

You get just two c2440 mistakes. Of course you can't convert a member function type to any of the other slightly different types, simply put, each member function pointer is a unique type and cannot be converted to any other type. Even if the definitions of two classes are identical, they cannot be converted between their corresponding member function pointers. This is somewhat analogous to the type of the struct, each of which is the only type, but the difference is that the type of the struct pointer can be cast. With these special usages and strict restrictions, the pointer to the class member function actually becomes useless. This is why we usually do not see "::*", ". *" and "->*" in the code.

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.