473,394 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

function pointer

Hello -
I am using a library that takes a function pointer as an argument. Is the code below not possible?
int library_func(void (*func)(int, short, void *));
I am trying to do this...

class Test {
public:
Test();
void a(int, short, void *);
void loop(void);
};

Test::Test()
{
}

void Test::a(int a, short b, void *c)
{
}

void Server::loop(void)
{
void (*func)(int, short, void *) = a;

library_function(func);
}

int main(void)
{
Test s;
s.loop();

return (0);
}
Jul 19 '05 #1
9 7684

"David Hill" <da***@wmol.com> wrote in message news:20030624140611.71b47dfa.da***@wmol.com...
Hello -
I am using a library that takes a function pointer as an argument. Is the code below not possible?

void (*func)(int, short, void *) = a;


It is not possible. Member functions are different than regular functions.
You can't convert between the two. What object is the member function
going to be invoked on if you only have the member address?

If you can't change the interface to the library, you're going to have to wrap the
member function call in an ordinary function. You can squirrel away the pointer
to member and "this" pointer in that extra void* operand I suspect.
Jul 19 '05 #2
"Ron Natalie" <ro*@sensor.com> wrote in message
news:Q4********************@giganews.com...

"David Hill" <da***@wmol.com> wrote in message news:20030624140611.71b47dfa.da***@wmol.com...
Hello -
I am using a library that takes a function pointer as an argument. Is the code below not possible?
void (*func)(int, short, void *) = a;


It is not possible. Member functions are different than regular

functions. You can't convert between the two. What object is the member function
going to be invoked on if you only have the member address?

If you can't change the interface to the library, you're going to have to wrap the member function call in an ordinary function. You can squirrel away the pointer to member and "this" pointer in that extra void* operand I suspect.


What does that mean (You can squirrel away the pointer to member and "this"
pointer in that extra void* operand I suspect)?
(i'm a newb, sorry)
Jul 19 '05 #3

Declare a member function of you're object as static. This will keep it from
getting a "this" pointer, and you'll be able to point a C style function
pointer at it. Pass this to you're library. Additionally (and this is the
squirling away part), most of the time when you're doing this, the call that
tells the library where the function to callback is also includes a void*
that you can pass whatever you want to. This void* gets passed to the
function when it is called. Since you passed a static function, you have no
this pointer, and no way to get at the object. Pass the "this" pointer into
that void*, cast it into the object (using dynamic_cast<> for safety) inside
the callback function, and you can now get at you're object.

Nifty no?

Tony
"Jeremy" <th***********@hotmail.com> wrote in message
news:jH**********************@twister.tampabay.rr. com...
"Ron Natalie" <ro*@sensor.com> wrote in message
news:Q4********************@giganews.com...

"David Hill" <da***@wmol.com> wrote in message news:20030624140611.71b47dfa.da***@wmol.com...
Hello -
I am using a library that takes a function pointer as an argument. Is the code below not possible?
void (*func)(int, short, void *) = a;


It is not possible. Member functions are different than regular

functions.
You can't convert between the two. What object is the member function
going to be invoked on if you only have the member address?

If you can't change the interface to the library, you're going to have

to wrap the
member function call in an ordinary function. You can squirrel away
the pointer
to member and "this" pointer in that extra void* operand I suspect.

What does that mean (You can squirrel away the pointer to member and

"this" pointer in that extra void* operand I suspect)?
(i'm a newb, sorry)

Jul 19 '05 #4
Tony Di Croce wrote:

Declare a member function of you're object as static. This will keep it from
getting a "this" pointer, and you'll be able to point a C style function
pointer at it.


Well, maybe. But not portably. Member functions have C++ linkage, and a
C library expects functions with C linkage. Most compilers don't
distinguish between the two, so you can get away with doing this. But
it's better to use a non-member function and mark it extern "C".

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 19 '05 #5
>

void Server::loop(void)
{
// void (*func)(int, short, void *) = a;


it shud be:
void (*func)(int, short, void *) = &Test::a;
Jul 19 '05 #6
Chandra Shekhar Kumar wrote in news:3E***************@oracle.com:


void Server::loop(void)
{
// void (*func)(int, short, void *) = a;


it shud be:
void (*func)(int, short, void *) = &Test::a;


Not true, there is no conversion from member-function-pointer
to function-pointer, or from member-pointer to pointer for
that matter.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #7

"Chandra Shekhar Kumar" <ch***********@oracle.com> wrote in message
news:3E***************@oracle.com...


void Server::loop(void)
{
// void (*func)(int, short, void *) = a;


it shud be:
void (*func)(int, short, void *) = &Test::a;


No, this should not work.
My compiler issues this error message:
"'initializing' : cannot convert from 'void (__thiscall
Test::*)(int,short,void *)' to 'void (__cdecl *)(int,short,void *)'
There is no context in which this conversion is possible."
Jul 19 '05 #8

"Chandra Shekhar Kumar" <ch***********@oracle.com> wrote in message news:3E***************@oracle.com...


void Server::loop(void)
{
// void (*func)(int, short, void *) = a;


it shud be:
void (*func)(int, short, void *) = &Test::a;

It still won't work. You can't assign a pointer to member to pointer to (non-member) function
no matter how you qualify it.
Jul 19 '05 #9

"Jeremy" <th***********@hotmail.com> wrote in message news:jH**********************@twister.tampabay.rr. com...
"Ron Natalie" <ro*@sensor.com> wrote in message
news:Q4********************@giganews.com...

"David Hill" <da***@wmol.com> wrote in message

news:20030624140611.71b47dfa.da***@wmol.com...
Hello -
I am using a library that takes a function pointer as an argument. Is the code below not possible?
void (*func)(int, short, void *) = a;


It is not possible. Member functions are different than regular

functions.
You can't convert between the two. What object is the member function
going to be invoked on if you only have the member address?

If you can't change the interface to the library, you're going to have to

wrap the
member function call in an ordinary function. You can squirrel away the

pointer
to member and "this" pointer in that extra void* operand I suspect.


What does that mean (You can squirrel away the pointer to member and "this"
pointer in that extra void* operand I suspect)?
(i'm a newb, sorry)


struct WrapperHelper {
Test* obj;
void (TEST::*fp)(int, short, void*);
void* arg;
} ;

void Wrapper(int i, short s, void* vp) {
WrapperHelper* sp = static_cast<WrapperHelper*>(vp);
(sp->*func)(i, s, sp->arg);
delete sp;
}

void Test::SetupWrapper() {
WrapperHelper* sp = new WrapperHelper;
sp->obj = this;
sp->fp = &Test::func;
sp->arg = whatever.
library_func(&Wrapper, sp);
}

Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
27
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
54
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
10
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.