By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,033 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,033 IT Pros & Developers. It's quick & easy.

Problem with __cdecl

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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 ** );
Jul 22 '05 #2

P: n/a
Is there any way to cast int (Sample::*)(void *,int,char ** ,char ** );
into int (*)(void *,int, char **, char **);
thanks,

Jul 22 '05 #3

P: n/a
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

Jul 22 '05 #4

P: n/a

"Sagar Choudhary" <sa*************@gmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
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.
Jul 22 '05 #5

P: n/a

"Iguana" <ha*****@f2s.com> wrote in message
news:cq**********@news.freedom2surf.net...

"Sagar Choudhary" <sa*************@gmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
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.


Expand the thread first you say?

What a clever idea! ;)

Iguana
Jul 22 '05 #6

P: n/a
On Tue, 21 Dec 2004 20:37:53 -0800, Sagar Choudhary wrote:
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

Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.