CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Pointer to a member function

  1. #1
    Join Date
    Aug 2007
    Posts
    858

    Pointer to a member function

    I'm working on a class which will, among other things, take text input from the user which needs to be parsed. What I'd like to do is include a simple parser to handle basic operations, but make the option available to use a custom parser function if something more powerful is needed.

    What I'm doing is along these lines

    Code:
    class Foo
    {
    public:
      Foo( ) { SetParser( ); }
    
      void SetParser(void (*func)(const std::string &) = NULL)
      {
        if (!func)
        {
          Parser = &Foo::DefaultParse;
        }
        else
        {
          Parser = func;
        }
      }
    
    private:
      void (*Parser)(const std::string &);
    
      void DefaultParse(const std::string &input)
      {
        //stuff
      }
    };
    But I get the error
    "error C2440: '=' : cannot convert from 'void (__thiscall Foo::* )(const std::string &)' to 'void (__cdecl *)(const std::string &)'
    1> There is no context in which this conversion is possible"

    So is there any way to do have a pointer to a non-static member function? I can get around it if there isn't, but I'm more interested for academic reasons at this point.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Pointer to a member function

    Non-static member functions implicitly take the "this" pointer as a parameter.

    Easiest way to approach it is like so:

    Code:
    class Foo
    {
    public:
      Foo( ) { SetParser( ); }
    
      void SetParser(void (*func)(const std::string &) = NULL)
      {
        if (!func)
        {
          Parser = &Foo::DefaultParse;
        }
        else
        {
          Parser = func;
        }
      }
    
    private:
      void (*Parser)(const std::string &);
    
      void DefaultParse(const std::string &input)
      {
        //stuff
      }
      friend void DefaultParseFriend(Foo *obj, const std::string &input)
      {    obj->DefaultParse(input);    }
    };
    Alternatively, you could replace your simple function pointer with a full-up functor which could store the object pointer internally at construction time.

  3. #3
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: Pointer to a member function

    Hi.

    Function pointers are not the same type as member function pointers. Check this out.

    A slightly different design might solve your problem. Leave Parse as a function pointer. Make a change in the SetParser member function. However, don't set the parser during construction and in the "doParse" function you can check whether or not someone has set an alternative parser. Something like this:

    Code:
    #include <string>
    
    class Foo
    {
    public:
      Foo( ):Parser(0){}
    
      void SetParser(void (*func)(const std::string &))
      {   
          Parser = func;
      }
    
    private:
      void (*Parser)(const std::string &);
    
      void doParse(const std::string &input)
      {
         if (Parser)
         {
            //...
         }
         else
         {
           //default parse
         }
      }
    };
    If you do that, you might wanna implement a couple of other related member functions like ClearParser, GetParser, etc.

  4. #4
    Join Date
    Aug 2007
    Posts
    858

    Re: Pointer to a member function

    Ah yeah, it hit me right after I posted that the this pointer was probably the issue. I think what I may do is change the parser prototype to

    Code:
    void (*Parser)(const std::string &, void *);
    So then I can just make the default parser static and pass the this pointer, and the option to pass an argument to custom parse functions probably wouldn't hurt.

  5. #5
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Pointer to a member function

    What discussion is taken place here ?

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)