The difference between C pointer-to-function and C + + Pointer-to-memberfunction

Source: Internet
Author: User

When looking at Apue Figure1.10 found signal (SIGINT, sig_int) here Sig_int directly with the function name, but see Thinking-in-c++ vol.2 when found Mem_fun (&shape:: Draw) But the function name to take the address operation, feel in doubt to check the information, the following code can show some of the differences between the two

Resources:

Http://stackoverflow.com/questions/3050805/pointer-to-const-member-function-typedef

http://www.cplusplus.com/reference/functional/mem_fun1_t/

Http://www.cnblogs.com/taobataoma/archive/2007/08/30/875743.html

http://www.cplusplus.com/reference/functional/mem_fun/

Code:

1#include <functional>2#include <iostream>3#include <algorithm>4 5 6 using namespacestd;7 8 voidMyintArg);9 Ten classMyClass One { A  Public: -     voidMyintArg) {cout << arg <<Endl;} - }; the  - //Method -  -typedefvoidFunc_ptr (int);//Func_ptr and func_ptr2 nature is the same, choose which way of definition to see your preferences +typedefvoid(*FUNC_PTR2) (int);//Reference:http://www.cnblogs.com/qrlozte/p/4439002.html - voidDosomething_one (func_ptr ptr); + voiddosomething_two (func_ptr2 ptr); A  at //Method 3,4,5 -  - voidDosomething_three (void(MyClass::* my_ptr) (int));  -  -typedefvoidFunctype (int); -typedef functype MYCLASS::*Myclassfunctype; in  -typedefvoid(MyClass::* memberfunctype) (int); to  + voiddosomething_four (Myclassfunctype ptr); -  the voiddosomething_five (Memberfunctype ptr); *  $  Panax Notoginseng  - intMain () { the     /* + the essence of the method is the same. A Method 3,4,5 Nature is the same the     */ + Dosomething_one (my); - Dosomething_two (my); $Dosomething_three (&myclass::my); $Dosomething_four (&myclass::my); -Dosomething_five (&myclass::my); -     return 0; the}///:~ - Wuyi voidMyintArg) the { -cout << Arg <<Endl; Wu } -  About  $ voidDosomething_one (func_ptr ptr) - { -ptr1); - } A  + voiddosomething_two (func_ptr ptr) the { -ptr2); $ } the  the voidDosomething_three (void(MyClass::* my_ptr) (int)) the { theMyClass *obj =NewMyClass; -(OBJ-&GT;*MY_PTR) (3); in     Deleteobj; the } the  About voiddosomething_four (myclassfunctype ptr) the { theMyClass *obj =NewMyClass; the(OBJ-&GT;*PTR) (4); +     Deleteobj; - } the Bayi voiddosomething_five (memberfunctype ptr) the { theMyClass *obj =NewMyClass; -(OBJ-&GT;*PTR) (5); -     Deleteobj; the}

The difference between C pointer-to-function and C + + Pointer-to-memberfunction

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.