C + + Primer notes-class member pointers

Source: Internet
Author: User

1. When we initialize a member pointer or assign a value to a member pointer, the pointer does not point to any data. The member pointer specifies the member rather than the object to which the member belongs, and we provide the object information only when the member pointer is dereferenced.

2. Similar to normal function pointers, if a member has an overloaded problem, we must explicitly declare the function type to indicate exactly which function we want to use. As with pointers to data members, we use the. * or->* operator for pointers to member functions.

classtest{ Public:    voidADD (int){}    voidADD (Long){}    void Get() {}};auto PMF= &test::Get;//using Auto is the premise that the function does not accept any argumentsvoid(Test::* M_PF) (int) = &test::Add;//the add that points to the int versionvoid(Test::* m_pf1) (int) = Test::add;//error, no automatic conversion rule exists between member function and pointerintMain () {Test T; (T.*M_PF) (1);//take care not to forget *    return 0;}


3. Using a type alias or typedef makes the member pointers easier to understand.

classtest{ Public:    voidADD (int) {}};typedefvoid(Test::* M_PF) (int); M_PF m= &Test::add;usingTestadd =void(Test::*) (int); Testadd M_testadd= &Test::add;intMain () {Test T; (T.*M) (1); (T.*m_testadd) (1); return 0;}

4. One way to get a callable object from a pointer to a member function is to use the standard library template function. Typically, the object that executes the member function is passed to the implicit this parameter.

STD::VECTOR<STD::string> VEC = {"test"};std::function<  BOOL(const std::string&) > fp = &std::string:: Empty; bool b = fp (*vec.begin ());            // can be understood as (*vec.begin ()). *FP ();

5.MEM_FN can generate a callable object from a member pointer, unlike the function, MEM_FN can infer the type of the callable object based on the type of the member pointer, without specifying it as a user's display.

STD::VECTOR<STD::string> VEC = {"test"}; bool b = STD::MEM_FN (&std::string:: Empty) (*vec.begin ());        // correct, use. * Call Object     b = Std::mem_fn (&std::string:: Empty) (Vec.begin ());                // correct, use->* to invoke    the object


6. We can also use Binf to generate a callable object from a member function, similar to function, where the implicit formal parameters used to represent the execution object must be converted to display. Like MEM_FN, the first argument of a callable object generated by bind can be either a pointer or a reference.

STD::VECTOR<STD::string> VEC = {"test"}; bool b = Std::bind (&std::string:: Empty, std::p laceholders::_1) (*vec.begin ());        // correct b = Std::bind (&std::string:: Empty, std::p laceholders::_1) (Vec.begin ());                // correct

C + + Primer notes-class member pointers

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.