类成员函数作为函数指针 [英] Class member function as function pointer

查看:191
本文介绍了类成员函数作为函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,其成员函数之一实际上是一个函数指针。这样用户可以覆盖此函数做什么。我不幸运行这个函数有一些困难。我使用g ++编译。

I have a class and one of its member functions is actually a function pointer. That way the user can overwrite what does this function do. I unfortunately have some difficulties running this function. I use g++ to compile.

下面是代码:

    #include <iostream>

    using namespace std;

    double fcn_mod(const double &phit){
        return 1.2+phit;
    }

    class Object{
        public:
            Object() {
                fcn_ptr = (double (*)(const double &)) &Object::fcn_default;
            }
            double (*fcn_ptr)(const double &) = NULL;

        private:
            double fcn_default(const double &phit){
                return 3.2+phit;
            }
    };


    int main(){

        Object test1,test2;

        cout << (test1.*fcn_ptr)(2) << endl; // can not compile here

        test2.fcn_ptr = &fcn_mod;
        cout << (test2.*fcn_ptr)(2) << endl; // and here

        cout << "test" << endl;
    }

错误讯息是:

    erreur: ‘fcn_ptr’ was not declared in this scope

+++++ UPDATE 1 +++++

考虑到评论,这里是一个更干净的版本: / p>

Taking into account the comments, here is a cleaner version:

    #include <iostream>

    using namespace std;

    double fcn_mod(const double &phit){
        return 1.2+phit;
    }

    double fcn_default(const double &phit){
        return 3.2+phit;
    }

    class Object{
        public:
            double (*fcn_ptr)(const double &) = NULL;
            Object() {
                fcn_ptr = &fcn_default;
            }

    };


    int main(){

        Object test1,test2;

        cout << (test1.*fcn_ptr)(2) << endl; // can not compile here

        test2.fcn_ptr = &fcn_mod;
        //cout << (test2.*fcn_ptr)(2) << endl; // and here

        cout << "test" << endl;
    }

这也是一个更好的方法:

This one is also a better approach:

    #include <iostream>

    using namespace std;

    double fcn_mod(const double &phit){
        return 1.2+phit;
    }

    class Object{
        public:
            Object() {
                fcn_ptr = &fcn_default;
            }
            double (*fcn_ptr)(const double &) = NULL;
        private :
            static double fcn_default(const double &phit){
                return 3.2+phit;
            }
    };

    int main(){
        Object test1,test2;
        cout << (test1.*fcn_ptr)(2) << endl; // can not compile here
        test2.fcn_ptr = &fcn_mod;
        cout << (test2.*fcn_ptr)(2) << endl; // and here
        cout << "test" << endl;
    }

+++++ UPDATE 2 +++++

如果我想通过此功能访问类的成员怎么办?

What if I want to access members of the class through this function?

将无法从fcn_mod内访问类成员(aa)。

The simplest solution will not be able to give access to the class member (aa) from within fcn_mod.

#include <iostream>

using namespace std;

double fcn_mod(const double &phit){
    return 1.2 + phit + aa*0.001; // can not compile here
}

class Object{
    public:
        Object() {
            fcn_ptr = &Object::fcn_default;
            aa = 4;
        }
        double (*fcn_ptr)(const double &) = NULL;
        double aa;

    private:
        static double fcn_default(const double &phit){
            return 3.2 + phit + aa*0.001; // and here
        }
};

int main(){
    Object test1,test2;
    cout << (test1.fcn_ptr)(2) << endl;
    test2.fcn_ptr = &fcn_mod;
    cout << (test2.fcn_ptr)(2) << endl;
    cout << "test" << endl;
}

但是使用std :: bind和std :: function的解决方案。我可以传递一个static参数到函数指针?

But the solution using std::bind and std::function does not either. Can I pass a kind of "static" parameter to the function pointer?

我添加了一个网关函数。

I added a gateway function.

#include <iostream>

using namespace std;

double fcn_mod(const double &phit){
    return 1.2 + phit;
}

class Object{
    public:
        Object() {
            fcn_ptr = &Object::fcn_default;
            aa = 4;
        }
        double do_something(const double &phit){
            return this->fcn_ptr(phit+aa*0.001);
        }
        double (*fcn_ptr)(const double &);
        double aa;

    private:
        static double fcn_default(const double &phit){
            return 3.2 + phit;
        }
};

int main(){
    Object test1,test2;
    cout << test1.do_something(2) << endl; // can not compile here
    test2.fcn_ptr = &fcn_mod;
    cout << test2.do_something(2) << endl; // and here
    cout << "test" << endl;
}


推荐答案

具有作为函数指针的函数,而是具有作为函数指针的数据成员的类。这是一个重要的区别,因为函数不会收到指针,也不能访问受保护或私有成员。

You do not have a class with a "function which is a function pointer", but rather a class with a data member which is a function pointer. This is an important distinction as the function won't receive a this pointer, nor will it have access to protected or private members.

没有正确声明你的函数指针,但你需要使用 .fcn_ptr ,而不是。* fcn_ptr 。我相信这足以解决你的问题。

You've declared your function pointer correctly, but you need to use the .fcn_ptr rather than .*fcn_ptr. I believe this is sufficient to solve your problem.

请注意,你使用旧的C风格函数指针语法。您可能想要学习C ++函数指针替代方法,包括 std :: function

Do note that you're using old C style function pointer syntax. You may want to learn the C++ function pointer alternatives including std::function.

这篇关于类成员函数作为函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆