Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

function pointer call syntax

Posted on 2003-12-04
10
Medium Priority
?
407 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
Technology Partners: 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 600 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

Technology Partners: 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!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

722 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