1

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 *)'

1
  • ok, considering you haven't found the c++ faq, why do you want to use a pointer to member function instead of just calling the member itself? The class hints at some kind of design of a class hierarchy, would you please elaborate why you need this indirection? Commented Nov 18, 2013 at 8:24

1 Answer 1

2

You should simply change typedef.

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.