如何从静态成员函数调用成员函数的指针? [英] How to invoke pointer to member function from static member function?

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

问题描述

我需要一个标准函数指针调用的成员函数,所以我试图抽象这样的东西:

  class Sample {
public:
virtual void doSomething(void)= 0;
};


class A:public Sample {
void doSomething(void); // details ignored
};

class B:public Sample {
void doSomething(void); // details omitted
};


类执行器{
public:
Executor(Sample * sample)
:func(& sample-> doSomething)
{
}

static void * execute(void * data){
Executor * pX = data;

(pX-> * func)(); //错误无效的函数func从静态函数访问

(pX-> * pX-> func)(); //成员类型的错误指针void(Sample ::)()'
//与对象类型Executor不兼容
}

private:
void (Sample :: * func)(void);
};



int main(void){
A myA;
B myB;
Executor x0(& myA);
执行器x1(& myB);

externalInvoke(& Executor :: execute,& x0);
externalInvoke(& Executor :: execute,& x1);
}

exteriorInvoke Linux系统调用,它接受一个函数指针和一个数据指针。
我想使用一个静态成员函数和一个this指针作为数据。



...我不想要像 A B 具有静态成员。所以我的想法是创建一个接口类 Sample ,扩展 A B



我的问题是,我不知道如何从中调用成员函数的指针。问题是,你需要两个对象在<$ c中。

$ c> execute - 一个是 Executor 的实例,它将提供 func 另一个是将调用 func 示例的一个实例(一个派生类)因此,您必须在 Executor 内存储对象,而不是函数:

  class Executor {
public:
Executor(Sample * sample)
:obj(sample)
{
}

static void * execute(void * data){
Executor * pX = static_cast< Executor *>(data);

pX-> obj-> doSomething();
}

private:
Sample * obj;
};


int main(){//注意`void main()`不是合法的C ++
A myA;
B myB;
Executor x0(& myA);
执行器x1(& myB);

externalInvoke(& Executor :: execute,& x0);
externalInvoke(& Executor :: execute,& x1);
}

指向成员函数的指针(例如您的原始 void(Sample :: * func)())标识类中的一个函数,但不存储对象。您仍然需要提供一个来调用该函数。


I need to get a member function called by a standard function pointer, so I tried to abstract things like this:

class Sample {
public:
    virtual void doSomething(void) = 0;
};


class A : public Sample {
    void doSomething(void);     // details omitted
};

class B : public Sample {
    void doSomething(void);     // details omitted
};


class Executor {
public:
    Executor(Sample *sample)
     : func(&sample->doSomething)
    {
    }

    static void *execute(void *data) {
        Executor *pX = data;

        (pX->*func)();          // error invalid access of func from static function

        (pX->*pX->func)();      // error pointer to member type 'void (Sample::)()'
                                //       incompatible with object type 'Executor'
    }

private:
    void (Sample::*func)(void);
};



int main(void) {
    A   myA;
    B   myB;
    Executor x0(&myA);
    Executor x1(&myB);

    externallyInvoke(&Executor::execute, &x0);
    externallyInvoke(&Executor::execute, &x1);
}

externallyInvoke is a Linux system call, which takes a function pointer and a data pointer. I'd like to use a static member function together with a this-pointer as data.

... and I don't want classes like A or B to have static members. So my idea was to create an interface like class Sample, that gets extended by A and B.

My problem is that I don't know how to invoke the pointer to member function from inside the Executor::execute function.

解决方案

The problem is that you need two objects inside execute - one is the instance of Executor which will supply func, and the other is an instance of (a class derived from) Sample on which func will be invoked. So you have to store the object inside Executor, not the function:

class Executor {
public:
    Executor(Sample *sample)
     : obj(sample)
    {
    }

    static void *execute(void *data) {
        Executor *pX = static_cast<Executor*>(data);

        pX->obj->doSomething();
    }

private:
    Sample *obj;
};


int main() { // note that `void main()` is not legal C++
    A   myA;
    B   myB;
    Executor x0(&myA);
    Executor x1(&myB);

    externallyInvoke(&Executor::execute, &x0);
    externallyInvoke(&Executor::execute, &x1);
}

A pointer to member function (such as your original void (Sample::*func)()) identifies a function within a class, but does not store the object. You'd still need to provide one to call the function.

这篇关于如何从静态成员函数调用成员函数的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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