Problem with __cdecl

S

Sagar Choudhary

I m using a library function which need to be passed a function as
parameter.
If i declare the function as
int (void *, int, char **, char **)
it throws an error
cannot convert parameter from 'int (void *,int,char ** ,char ** )' to
'int (__cdecl *)(void *,int,char ** ,char ** )'

If the same function is moved out of the class or declared static
it works fine.
But i don't want the function to be declared static or moved out of the
class because i m using non static members of the class
I just mentioned the subject according to error.
Any solutions to this problem
 
K

Kurt Krueckeberg

I m using a library function which need to be passed a function as
parameter.
If i declare the function as
int (void *, int, char **, char **)
it throws an error
cannot convert parameter from 'int (void *,int,char ** ,char ** )' to
'int (__cdecl *)(void *,int,char ** ,char ** )'

If the same function is moved out of the class or declared static
it works fine.
But i don't want the function to be declared static or moved out of the
class because i m using non static members of the class
I just mentioned the subject according to error.
Any solutions to this problem

I member function of a class, if that is what you are trying to pass, is of
a different type than a regular (non-class) function or a static member
function.

If the library function is something like

typedef int (*CALLBACK)(void *,int,char ** ,char ** );
void takes_callback(CALLBACK *pf, void *,int,char ** ,char ** );

then you cannot you a member funciton as a callback....
class Sample {
public:
//. . .
int sample_func(void *,int,char ** ,char ** );
private:
// . . .
};
//. . .
takes_callback(&sample_func, vp, 5, &pchar, &pchar);

....because sample_func is of type pointer to member of Sample (and
taking void *,int,char ** ,char ** ). That is, it is of type
int (Sample::*)(void *,int,char ** ,char ** );
and not of type
int (*)(void *,int,char ** ,char ** );
 
S

Sagar Choudhary

Is there any way to cast int (Sample::*)(void *,int,char ** ,char ** );
into int (*)(void *,int, char **, char **);
thanks,
 
J

JH Trauntvein

A member function is essentially different than a stand-alone function
in that it always receives an implicit parameter, this, in its stack
frame. You could probably cast away the differences to the point that
the compiler will be happy but you will wind up crashing your program
in a rather spectacular way when your stack becomes corrupted as the
call-back is made. You are better off adjusting your architecture to
the limitations of the call-back mechanism than kicking against the
pricks.

Regards,

Jon Trauntvein
 
I

Iguana

Sagar Choudhary said:
I m using a library function which need to be passed a function as
parameter.
If i declare the function as
int (void *, int, char **, char **)
it throws an error
cannot convert parameter from 'int (void *,int,char ** ,char ** )' to
'int (__cdecl *)(void *,int,char ** ,char ** )'

If the same function is moved out of the class or declared static
it works fine.
But i don't want the function to be declared static or moved out of the
class because i m using non static members of the class
I just mentioned the subject according to error.
Any solutions to this problem

If you want to pass it as a dynamic function... the function in the library
needs to be declared int(T::Function *)(void *,int,char**,char**)

So I don't think its gonna work. I'm thinking its something to do with how
the class uses functions i.e. first parameter in the member function is
actually a pointer to the object tho is not shown

Course I also think the world is flat....

Cheers,
Iguana.
 
I

Iguana

Iguana said:
If you want to pass it as a dynamic function... the function in the
library needs to be declared int(T::Function *)(void *,int,char**,char**)

So I don't think its gonna work. I'm thinking its something to do with
how the class uses functions i.e. first parameter in the member function
is actually a pointer to the object tho is not shown

Course I also think the world is flat....

Cheers,
Iguana.

Expand the thread first you say?

What a clever idea! ;)

Iguana
 
J

Jay Nabonne

I m using a library function which need to be passed a function as
parameter.
If i declare the function as
int (void *, int, char **, char **)
it throws an error
cannot convert parameter from 'int (void *,int,char ** ,char ** )' to
'int (__cdecl *)(void *,int,char ** ,char ** )'

If the same function is moved out of the class or declared static
it works fine.
But i don't want the function to be declared static or moved out of the
class because i m using non static members of the class
I just mentioned the subject according to error.
Any solutions to this problem

As stated by others, you can't pass a member function where a non-member
function is required.

However, these callback mechanisms often provide a means to pass "user
data" when registering the callback which is then passed to your function
when called. I can't tell from your function signature (because the
argument names have been stripped), but the first "void*" parameter may be
such a thing. (Just guessing..) If so, use a static method, pass the
object you wish to use as the user data, and cast it back from the void*
in the callback to invoke a member function on that object.

HTH

- Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top