C + + class member function pointer class member data pointer

Source: Internet
Author: User

normal function pointer: "Return_type (*ptr_name) (para_types)"

Class member function pointer: "Return_type (class_name::* ptr_name) (para_types)"

Class data member pointer: "type class_name::* ptr_name";

C + +:

1 classDemo2 {3  Public:4Demo ():d ATA ( -)5     {6 7     }8     intdata;9     intShowintAintb)Ten     { One         returnA +b; A     } - }; -  the  -  - intMainintargcChar**argv) - { + Demo A; -demo* b=NewDemo; +     //Complete Declaration and assignment A     intDemo::* int_ptr=&Demo::d ata; at     //No data information after assignment, need object dereference to get data -std::cout<<a.*int_ptr<<Std::endl; -std::cout<<b->*int_ptr<<Std::endl; -  -      -     //class member function pointers in     int(Demo::* ptr) (int,int) =&demo::show; -std::cout<< (A.*PTR) (1,2) <<Std::endl; to  +     return 0; -}


STL algorithms sometimes require a function of a class member, whereas a class member function pointer is not a callable object,functor<> bind MEM_FN is different in its own way, but internally it is implicitly passed the this pointer to fetch data by dereference or call function

A/C + + extension:

1 //The member function pointer uses the2 3 class Screen4 {5  Public:6     enumACTION7     {8 Up_, Down_, Left_, Right_9     };Ten  OneScreen () =default; A  -Screen &Move (ACTION para) -     { the         return( This->*Menu[para]) (); -     } -  - Private: +     usingaction= -Screen &-screen::*)(); +  A  at     StaticAction menu[]; -  -Screen &Up () -     { -Std::cout <<" up"<<Std::endl; -         return* This; in     } -  toScreen &Down () +     { -Std::cout <<" Down"<<Std::endl; the         return* This; *     } $ Panax NotoginsengScreen &Left () -     { theStd::cout <<" Left"<<Std::endl; +         return* This; A     } the  +Screen &Right () -     { $Std::cout <<" Right"<<Std::endl; $         return* This; -     } -  the }; - WuyiScreen::action screen::menu[]{&screen::up, &screen::D Own, &screen::left, &Screen::right}; the  -  Wu intMain () - { About Screen obj; $ obj. Move (SCREEN::UP_); - obj. Move (screen::D own_); - obj. Move (Screen::left_); - obj. Move (screen::right_); A obj. Move (screen::right_); +  the  -     return 0; $ } the  the  the  the#include <iostream> -#include <vector> in#include <functional> the#include <algorithm> the#include <string> About  the //class member function pointers are not callable objects, generic STL algorithms need to wrap class member function pointers as callable objects the //These three packages are internally called by obtaining the object returned by the container, ". *"/"->*"; the //1.std::function< member function type (the first parameter is set to the type of the object itself) > fcn=&std::xxxx::xxx; Difference: The first parameter is the incoming object type, depending on whether the container is a reference or a pointer selection (. */->*) + //2.std::bind (&std::string::empty,std::p laceholders::_1) Differences: You can execute member functions by pointers or objects; - //3.std::mem_fn (&std::string::empty) c++11, Difference: member functions can be executed by pointers and objects the //4. can use LAMDA call;Bayi  the  the classStr - { -  Public: theSTD::stringstr; the  theSTR (ConstSTD::string&str_): Str (STR_) the     { -  the     } the  theSTR (Const Char*str_): Str (STR_)94     { the  the     } the 98     BOOLEmpty ()Constnoexcept About     { -         returnstr.empty ();101     }102 };103 104  the intMain ()106 {107 108Std::vector<str> str_vec{"xa","Sad","","","","Qqewhegr","AQDQ"};109  the 111std::function<BOOL(ConstSTR &) > fn = &Str::empty; the 113std::size_t empty_size_function=std::count_if (Str_vec.begin (), Str_vec.end (), FN); the  thestd::size_t empty_size_bind=std::count_if (Str_vec.begin (), Str_vec.end (), Std::bind (&str::empty,std::p laceholders::_1)); the 117std::size_t empty_size_mem_fn=std::count_if (Str_vec.begin (), Str_vec.end (), STD::MEM_FN (&str::empty));118 119  -Std::cout << empty_size_function <<Std::endl;121Std::cout << Empty_size_bind <<Std::endl;122Std::cout << EMPTY_SIZE_MEM_FN <<Std::endl;123 124  the 126std::size_t size = std::count_if (Str_vec.begin (), Str_vec.end (), [] (Conststr& str) {returnstr.empty ();});127Std::cout << Size <<Std::endl; - 129  the     return 0;131}

C + + class member function pointer class member data pointer

Related Article

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.