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

x
?
Solved

Pointer to function

Posted on 1999-09-26
14
Medium Priority
?
306 Views
Last Modified: 2010-04-16
I have function

void Function (char* string);

I need get pointer (void*) to this function
how?
0
Comment
Question by:cdesigner
[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
  • 2
  • +2
14 Comments
 
LVL 2

Expert Comment

by:gothick
ID: 2075785
Why do you need void*?

Just "Function" should evaluate to a pointer to the function, e.g.

void Function(char* string);
....
void (*fptr)(char*); /* Declare fptr as a pointer to a function returning void */
                          /* and taking a char* parameter */
fptr = Function;

If you want it void*, then just cast it.
0
 
LVL 9

Author Comment

by:cdesigner
ID: 2075798
but I have definition in header

typedef void (WINAPI *pFunction)(char *string);

and you method not compile....

error C2440: '=' : cannot convert from 'void (__stdcall CMyClassDlg::*)(char *)' to 'void (__stdcall *)(char *)'
0
 
LVL 2

Expert Comment

by:gothick
ID: 2075804
How about you say what you're actually trying to do.  It seems like you actually want a pointer to a _member_ function, which is different.  You'll also have a problem passing a pointer to a member function to something which doesn't expect it, as the implied first parameter (this) will not be expected.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 9

Author Comment

by:cdesigner
ID: 2075812
how I can do?

function to need pointer :

void WINAPI Set (UINT Code, void *pFunc);

help please
0
 
LVL 9

Author Comment

by:cdesigner
ID: 2075838
send me request for answer
I send you points
0
 
LVL 22

Expert Comment

by:nietod
ID: 2075852
gothick is right.  The problem is that the function is a non-static member of a class.  A pointer to a non-static member function is different than a pointer to a non-member function and there is no posibility to convert between them.  You must make the member function a static member function or you must use a special type of pointer called a "pointer-to-member"  This type of pointer is very different than other pointers and cannot be converted to  a "regular" pointer.  

It would help if we knew what you are trying to do.
0
 
LVL 6

Expert Comment

by:WxW
ID: 2076226
You 're using Borland with non-borland extensions maybe . Try casting using (*pfunction) . For example , some borland modes require the cast of (DLGPROC) , (WNDPROC) etc in "special" function pointers.


0
 
LVL 9

Author Comment

by:cdesigner
ID: 2076243
Now I understood my source , this other problem
0
 
LVL 1

Expert Comment

by:ramshakal
ID: 2096358
>void Function (char* string);
 > I need get pointer (void*) to this function how?

You can do this simply by
void (*fp) (char* string);

fp getpointer(void*)
{
   fp = Function;
   return fp;
}

If it is a C function and not a functon of C++ Class.

>how I can do?
>      function to need pointer :
>    void WINAPI Set (UINT Code, void *pFunc);
>      help please

for this you should do like this
void WINAPI Set(UINT Code, (void (*fp) (char* string)) )
0
 
LVL 22

Expert Comment

by:nietod
ID: 2096671
ramshakal,  have you read the question history?
0
 
LVL 9

Author Comment

by:cdesigner
ID: 2097816
question answered by gothick
0
 
LVL 2

Expert Comment

by:gothick
ID: 2097836
Should I repost my answer for you to grade?
0
 
LVL 9

Author Comment

by:cdesigner
ID: 2097944
yes
send me answer
I send you points
0
 
LVL 2

Accepted Solution

by:
gothick earned 60 total points
ID: 2098170
Thanks.  Here's my previous two answers pasted together as one, for completeness.  


Why do you need void*?

       Just "Function" should evaluate to a pointer to the function, e.g.

       void Function(char* string);
       ....
       void (*fptr)(char*); /* Declare fptr as a pointer to a function returning void */
                                 /* and taking a char* parameter */
       fptr = Function;

       If you want it void*, then just cast it.

It seems like you actually want a pointer to a _member_ function, which is different.  You'll also have a problem passing a pointer to a member function to something which doesn't expect it, as the implied first parameter (this) will not be expected.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

  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 …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

670 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