Solved

function pointer call syntax

Posted on 2003-12-04
10
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

752 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