Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

function pointer call syntax

hello.  

I have a class Parser and I want to call function pointers in the global namespace from with an argument of Parser.  
The unworking code I have now is:

class Parser
{
public:
        typedef void (*handler) (*this );

and from handler.h I have defined:

void * doVariable_second (Parser *);
void * doInclude_second (Parser *);
void * doIf (Parser *);
void * dodoIf_second (Parser *);

}

compilation errors:

In file included from Parser.cpp:10:
Parser.h:29: invalid use of `this' at top level
Parser.h:29: typedef declaration includes an initializer
Parser.h:29: typedef `handler' is initialized
Parser.cpp: In member function `virtual void Parser::set_handlers()':
Parser.cpp:56: invalid conversion from `void*(*)(Parser*)' to `void*'
Parser.cpp:56:   initializing argument 2 of `void
   Parser::addFirstHandler(std::basic_string<char, std::char_traits<char>,
   std::allocator<char> >, void*)'
Parser.cpp:57: invalid conversion from `void*(*)(Parser*)' to `void*'



I have one question:
how can I pass a class object as an instance to a member function using this pointer?
and another (i guess):
should I separate the function pointers into another class, contain the class in class parser, or leaeve them global?

note that using function pointers I was able to call
void
Parser::set_handlers ()
{
if (debug > 0) cerr << "Parser::set_handlers() " << endl;
        addFirstHandler ("if", doIf);

and so on for other function handlers.  

prototype:

void addFirstHandler (string , handler );

the above populated

  map < string, handler > first_handler_map;

which I could then search for string match and call the function.  But, how about passing in a Parser object.
0
joesp
Asked:
joesp
  • 6
  • 4
1 Solution
 
jkrCommented:
>>typedef void (*handler) (*this );

should read

typedef void (*handler) (Parser* );
0
 
joespAuthor Commented:
hello.

this is killing me.  Too many asterisks for me to get.

Parser.cpp: In member function `virtual void Parser::set_handlers()':
Parser.cpp:56: invalid conversion from `void*(*)(Parser*)' to `void
   (*)(Parser*)'
0
 
joespAuthor Commented:
nevermind, I had typedef void (*handler) (Parser* );
but my handler functions all returned simple void. let me change that.  
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jkrCommented:
>>Parser.cpp:56: invalid conversion from `void*(*)(Parser*)' to `void  (*)(Parser*)'

Do you want the handlers to return 'void*' or 'void'? If the first one applies, you need to use

typedef void* (*handler) (Parser* );


0
 
joespAuthor Commented:
yes, i changed them all to typedef void* (*handler) (Parser* ); though I don't know the difference between returning void and a void pointer.  Well, this function pointer thing may seem to be going well for me at the moment, but my other tries have come back to bite me, so let me keep compiling and fixing code.   I'm tempted to close the question, but maybe in an hour if nothing bites me.
0
 
joespAuthor Commented:
how can I call it?  in this, tag.type is the type of markup I want to process, for example, <doForEachVector ...

              (first_handler_map[tag.type]) (this);

don't work, it's cryptic to me:
Parser.cpp: In member function `virtual void Parser::parse()':
Parser.cpp:324: `(this + 36)->std::map<_Key, _Tp, _Compare,
   _Alloc>::operator[](const _Key&) [with _Key = std::string, _Tp =
   void*(*)(Parser*), _Compare = std::less<std::string>, _Alloc =
   std::allocator<std::pair<const std::string, void*(*)(Parser*)> >](((&tag) +
   32))' cannot be used as a member pointer, since it is of type `
   void*(*)(Parser*)'

declarations are as above:
        map < string, handler > first_handler_map;      //
0
 
jkrCommented:
How *are* you calling them? The correct syntax would be

Parser* p = this;
map < string, handler >::const_iterator i;
i = first_handler_map.find("somestring");

p->(i->second) ( this);

0
 
joespAuthor Commented:
Is it not then possible to pass this pointer to a function pointer even if the function pointer is not a member function?  
I get the same error.  

Parser.cpp: In member function `virtual void Parser::parse()':
Parser.cpp:324: `(this + 36)->std::map<_Key, _Tp, _Compare,
   _Alloc>::operator[](const _Key&) [with _Key = std::string, _Tp =
   void*(*)(Parser*), _Compare = std::less<std::string>, _Alloc =
   std::allocator<std::pair<const std::string, void*(*)(Parser*)> >](((&tag) +
   32))' cannot be used as a member pointer, since it is of type `
   void*(*)(Parser*)'

Maybe you forgot (or I didn't say) that the function pointers are not member functions?  
I tried just
Parser* p = this;
map < string, handler >::const_iterator i;
i = first_handler_map.find("somestring");

(i->second) (p);

to generate above errors.  

If I try to recode function pointers as member functions I get other errors, I want to make sure first that there is no other way to try calling the handler with a this pointer.

0
 
jkrCommented:
>>Is it not then possible to pass this pointer to a function pointer even
>>if the function pointer is not a member function?

Sure.

>>Maybe you forgot (or I didn't say) that the function pointers are not member functions?  

Well, in your first post, they are declared within the class :o)

It is actually easier if they are not members:


Parser* p = this;
map < string, handler >::const_iterator i;
i = first_handler_map.find("somestring");
handler theHandler = i->second;

(theHandler) ( p);



0
 
joespAuthor Commented:
holy crap! it compiles!  very slowly, but it does compile.  good job!  

p < string, handler >::const_iterator i;
i = first_handler_map.find("somestring");
handler theHandler = i->second;
(theHandler)(this);


that works even (er, compiles, yes, works, maybe) if I shorten to

         (first_handler_map[tag.type])(this);
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now