Solved

function pointer call syntax

Posted on 2003-12-04
10
401 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

932 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

12 Experts available now in Live!

Get 1:1 Help Now