CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2009
    Posts
    65

    Function Pointer & _closure

    The trouble I'm facing is when I assign a function pointer to another.

    Eg:

    Code:
    class foo
    {
       public:
          bool (*myFunc)();
    };
    
    class Main
    {
       public:
          void Procedure();
          bool someFunc();
    };
    
    bool Main::someFunc ()
    {
       .
       .
    }
    
    void Main::Procedure()
    {
       foo Obj;
       Obj.myFunc = &someFunc;  //Error
    }
    On assigning I get the error:
    Cannot convert bool(*(_closure)())() to bool(*)(), but I don't know how to fix it. I know that it has something to do with the class owning the function & the function not being accessible without the class object, but how to fix it?

    P.S. If the function is a global, then Obj.myFunc = &someGlobalFunc; works out fine

    Any help is greatly appreciated.
    Regards,
    Nisheeth Barthwal

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Function Pointer & _closure

    Code:
          bool (*myFunc)();
    This is not a pointer to a non-static member function. This is a pointer to a non-class function, or static function.
    Code:
    bool Main::someFunc ()
    {
        return true;
    }
    
    void Main::Procedure()
    {
       foo Obj;
       Obj.myFunc = &someFunc;  //Error
    }
    Obj.myFunc is a non-static member function. You cannot assign a pointer of a non-static member function to a pointer to a static or non-class function.

    http://www.parashift.com/c++-faq-lit...o-members.html

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jun 2009
    Posts
    65

    Re: Function Pointer & _closure

    Thank you Paul for that link, I'm currently going through it.

    Regards,
    Nisheeth Barthwal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured