Calling C++ method pointers

I'm having a problem trying to a C++ method pointer.

There' lots of examples on the net of using simple C function pointers,
but working examples of passing C++ method pointers seem to be non-existent.

In the following snippet, I'm trying to create a std:map of sub-parsers,
then call one of the sub-parsers stored in the map.

I think I've worked out all the syntax issues except for line 53.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <map>
#include <string>
#include <utility>
#include <iostream>
using namespace std;

class Parser 
{
    struct sub_parser_info_t                    //  Filled in by register_sub_parser
    {
        string some_info;
        bool (Parser::*sub_func)(const string& str);    //  Ptr to sub parser function
    };

    map<char, sub_parser_info_t>     m_map;     //  Collection of sub parsers

public:
    //  Do the work
    bool sub_parser_a(const string& str)
    {   return true;
    } 
    bool sub_parser_b(const string& str)
    {   return true;
    }
    bool sub_parser_c(const string& str)
    {   return true;
    }
  
    //  Add a sub parser to the map
    bool register_sub_parser(char ltr, bool (Parser::*parser)(const string& str))
    {
        sub_parser_info_t        temp;
        pair<map<char, sub_parser_info_t>::iterator, bool>     rslt;

        temp.sub_func = parser;
        pair<char, sub_parser_info_t>    pr(ltr, temp);
        rslt = m_map.insert(pr);
        if (!rslt.second)
            return false;               //  Failed to register switch       
        return true;                    //  Added sub_parser
    }

    bool do_parse(const string& str)
    {
        pair<map<char, sub_parser_info_t>::iterator, bool>      rslt;
        map<char, sub_parser_info_t>::iterator                  iter;
        bool (Parser:: * fp)(const string & str);

        iter = m_map.find(str[0]);
        if (iter == m_map.end())
            return false;               //  Handler not found
        fp = iter->second.sub_func;
        if (! fp(str))                  //  Do the sub parse
            return false;               //  sub_parser failed        
        return true;                    //  sub parse successful
    }
};

int main()
{
    Parser  parser;
    const string str = "some text";
    
    parser.register_sub_parser('a', &Parser::sub_parser_a);
    parser.register_sub_parser('b', &Parser::sub_parser_b);
    parser.register_sub_parser('c', &Parser::sub_parser_c);
    parser.do_parse(str);
}


The problem is at line 53. The compiler doesn't like the function call.

Error	C2171	'*': illegal on operands of type 'bool (__cdecl Parser::* )(const std::string &)'	53	
Error	C2064	term does not evaluate to a function taking 1 arguments		53	
Line 53 should be:
if (! (this->*fp)(str)) // Do the sub parse
You need a special operator ->* or .* to call a pointer to member. The arrow-star needs an pointer-to-object on its left side and a pointer-to-member on the right. The "extra" parentheses are required.
Last edited on
Topic archived. No new replies allowed.