简体   繁体   中英

C++ function pointer (class member) to non-static member function of a template class

I have some problem figuring out if it is possible to implement function pointer to non-static member functions to be used within the class itself. I have a simplified code looks like following which works fine where function "func1" is a static member function when the Start function is called:

Template<class F> 
class MyClass
{
    public: 
        typedef void (*FuncPtrType)(float*);

        void Start()
        {
            this->Run(Func1);
        }

    protected:
        static void Func1(float arg[]){ // some code }

        void Run(FuncPtrType func){ // some code }
}

So my questions if it is even possible to make "func1" non-static? If so, what do I need to do to make it compile. I tried the following code:

Template<class F> 
class MyClass
{
    public: 
        typedef void (*FuncPtrType)(float*);

        void Start()
        {
            this->Run(&MyClass<F>::Func1);
        }

    protected:
        void Func1(float arg[]){ // some code }

        void Run(FuncPtrType func){ // some code }
}

It is throwing me errors in compilation like: error c2664: ... can not convert from 'void (MyClass::* )(float*)' to 'void (__cdecl *)(float *)'

You should simply change typedef .

typedef void (MyClass::*FuncPtrType)(float*);

Your code doesn't work, since pointer to function != pointer to member-function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM