C + +: pointers to class member functions

Source: Internet
Author: User

What is the difference between a pointer to a function and a pointer to a class member function?

int f (char A, float b);
If it is a normal function, then its type is int (*) (char, float);
If it is a non-static member function, its type is int (ClassName::*) (char, float);
If it is a static member function, its type is the same as a normal function.

How to pass member functions of a class to event handler, thread start function, etc.?

The member function of a class must be combined with an instance of a class to make sense. You can write a function to wrap the class's member functions.

class X {    public:    void  f ();     Static void SF ();}; x x*; void wapper_f () {x->F ();} int Main () {    //  OK    /  OK}

How do I make it easier to create a type that executes a class member variable?

With a typedef.

class Fred {    public:        int f (char  x);         int g (charint (Fred::* fredfn) (char  x); void f () {    = &fred::f;} void F2 (FREDFN fn) {...} FREDFN f3 () {...}


How to make it easier to call a class member function that is pointed to by a pointer?
#define Call_member_fn (object, Prttomember) ((object). * (prttomember))void Usercode (Fred &Fred, Fredfn FN) {     int ans = call_member_fn (Fred, FN) (3);}

Original reference: Https://isocpp.org/wiki/faq/pointers-to-members#fnptr-vs-memfnptr-types

C + +: pointers to class member functions

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.