Why would one use function pointers to member method in C++?

前端 未结 10 2159
情书的邮戳
情书的邮戳 2021-02-10 23:48

A lot of C++ books and tutorials explain how to do this, but I haven\'t seen one that gives a convincing reason to choose to do this.

I understand very well why functio

相关标签:
10条回答
  • 2021-02-11 00:28

    Here is a typical scenario we have here. We have a notification framework, where a class can register to multiple different notifications. When registering to a notification, we pass the member function pointer. This is actually very similar to C# events.

    class MyClass
    {
        MyClass()
        {
            NotificationMgr::Register( FunctionPtr( this, OnNotification ) );
        }
        ~MyClass()
        {
            NotificationMgr::UnRegister( FunctionPtr( this, OnNotification ) );
        }
    
        void OnNotification( ... )
        {
            // handle notification
        }
    };
    
    0 讨论(0)
  • 2021-02-11 00:31

    Functors are not a priori object-oriented (in C++, the term “functor” usually means a struct defining an operator () with arbitrary arguments and return value that can be used as syntactical drop-in replacements to real functions or function pointers). However, their object-oriented problem has a lot of issues, first and foremost usability. It's just a whole lot of complicated boilerplate code. In order for a decent signalling framework as in most dialog frameworks, a whole lot of inheritance mess becomes necessary.

    Instance-bound function pointers would be very beneficial here (.NET demonstrates this amply with delegates).

    However, C++ member function pointers satisfy another need still. Imagine, for example, that you've got a lot of values in a list of which you want to execute one method, say its print(). A function pointer to YourType::size helps here because it lets you write such code:

    std::for_each(lst.begin(), lst.end(), std::mem_fun(&YourType::print))
    
    0 讨论(0)
  • 2021-02-11 00:37

    It is like using lambdas. You always can pass all necessary local variables to a simple function, but sometimes you have to pass more then one of them.

    So using member functions will save you from passing all necessary member fields to a functor. That's all.

    0 讨论(0)
  • 2021-02-11 00:38

    In the past, member function pointers used to be useful in scenarios like this:

    class Image {
        // avoid duplicating the loop code
        void each(void(Image::* callback)(Point)) {
            for(int x = 0; x < w; x++)
                for(int y = 0; y < h; y++)
                    callback(Point(x, y));
        }
    
        void applyGreyscale() { each(&Image::greyscalePixel); }
        void greyscalePixel(Point p) {
            Color c = pixels[p];
            pixels[p] = Color::fromHsv(0, 0, (c.r() + c.g() + c.b()) / 3);
        }
    
        void applyInvert() { each(&Image::invertPixel); }
        void invertPixel(Point p) {
            Color c = pixels[p];
            pixels[p] = Color::fromRgb(255 - c.r(), 255 - r.g(), 255 - r.b());
        }
    };
    

    I've seen that used in a commercial painting app. (interestingly, it's one of the few C++ problems better solved with the preprocessor).

    Today, however, the only use for member function pointers is inside the implementation of boost::bind.

    0 讨论(0)
提交回复
热议问题