Solved

function pointer call syntax

Posted on 2003-12-04
10
399 Views
Last Modified: 2010-04-01
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
Comment
Question by:joesp
  • 6
  • 4
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9876490
>>typedef void (*handler) (*this );

should read

typedef void (*handler) (Parser* );
0
 
LVL 1

Author Comment

by:joesp
ID: 9876505
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
 
LVL 1

Author Comment

by:joesp
ID: 9876517
nevermind, I had typedef void (*handler) (Parser* );
but my handler functions all returned simple void. let me change that.  
0
 
LVL 86

Expert Comment

by:jkr
ID: 9876519
>>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
 
LVL 1

Author Comment

by:joesp
ID: 9876548
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:joesp
ID: 9876595
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
 
LVL 86

Expert Comment

by:jkr
ID: 9876624
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
 
LVL 1

Author Comment

by:joesp
ID: 9876736
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
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 9876794
>>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
 
LVL 1

Author Comment

by:joesp
ID: 9876874
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now