Casting a method pointer to a function pointer. 
Author Message
 Casting a method pointer to a function pointer.

I have to use a library and to call a function foo with a pointer
function in the argument

I have to pass to this function foo a method of an object.
( it's the object who call the function foo ).

I have sucess compile it with gcc under unix.

But under window's with mvsc 5 , I have problem:
So I have define two typedef :
typedef void (* BEGINTAG)(void *,const char *,const char **);^M
typedef void (* ENDTAG)(void *,const char *);^M

Corresponding of the type of the function pointer

The prototype of the method to caster is
virtual void OXMLendtag(const char *name);
virtual void OXMLbegintag(const char *name,const char **atts);^M

I have a variable who call testalacon , to try passing by a variable
instead of by a method directly.

The prototype of testalacon:

     void (OXML::* testalacon)(const char *name,const char **atts);

There're two function pointer declare in the object.

     BEGINTAG        startfunc;^M
     ENDTAG          endfunc;

I set testalacon tothe pointer method:
testalacon=o->OXMLbegintag;
( no problemo ).

But MSVC 5 make me deux errors of compilation:
J:\SRC\xml\OXML.cpp(14) : error C2440: 'type cast' :
cannot convert from 'void (OXML::*)(const char *,const char ** )'
to 'void (__cdecl *)(void *,const char *,const char ** )'

For :
startfunc=(BEGINTAG)testalacon;

There is no context in which this conversion is possible
J:\SRC\xml\OXML.cpp(16) : error C2440: 'type cast' :
cannot convert from 'overloaded function type'
to 'void (__cdecl *)(void *,const char *)'
None of the functions with this name in scope match the target type

For :
endfunc=(ENDTAG)o->OXMLendtag;

How could I cast a method pointeur to a function pointer ?
( I guess it is my problem ).
Coudl somebody help me ?

Thank's a lot.

PS: Please excuse my english I'm french.

charles vidal.



Tue, 28 Jan 2003 03:00:00 GMT  
 Casting a method pointer to a function pointer.

Quote:

> How could I cast a method pointeur to a function pointer ?
> ( I guess it is my problem ).

Well, you can't really do that in a safely manner, and under some
compilers, it's simply impossible. This is because function pointers and
pointers to members are very different things. The latter is often
implemented as a struct that contains offsets to class members rather
than memory addresses.

The classic solution is to make your member functions static. If this
isn't an option, there's a more complex solution:
http://www.inquiry.com/techtips/cpp_pro/10min/10min0800.asp

HTH,

Danny Kalev

"The ANSI/ISO C++ Professional Programmer's Handbook"
http://www.amazon.com/exec/obidos/ASIN/0789720221

Quote:
> Coudl somebody help me ?

> Thank's a lot.

> PS: Please excuse my english I'm french.

> charles vidal.



Tue, 28 Jan 2003 03:00:00 GMT  
 Casting a method pointer to a function pointer.

Quote:

> I have to use a library and to call a function foo with a pointer
> function in the argument

> I have to pass to this function foo a method of an object.
> ( it's the object who call the function foo ).

> I have sucess compile it with gcc under unix.

This just shows that gcc doesn't do enough error-checking by default.

<snip>

Quote:
> But MSVC 5 make me deux errors of compilation:
> J:\SRC\xml\OXML.cpp(14) : error C2440: 'type cast' :
> cannot convert from 'void (OXML::*)(const char *,const char ** )'
> to 'void (__cdecl *)(void *,const char *,const char ** )'

> For :
> startfunc=(BEGINTAG)testalacon;

> There is no context in which this conversion is possible
> J:\SRC\xml\OXML.cpp(16) : error C2440: 'type cast' :
> cannot convert from 'overloaded function type'
> to 'void (__cdecl *)(void *,const char *)'
> None of the functions with this name in scope match the target type

> For :
> endfunc=(ENDTAG)o->OXMLendtag;

> How could I cast a method pointeur to a function pointer ?
> ( I guess it is my problem ).

<snip>

Read what the compiler tells you - the conversion is not possible.
You must never cast function pointers or member function pointers.
Instead, correct the BEGINTAG and ENDTAG types:

    typedef void (OXML::* BEGINTAG)(const char *,const char **);
    typedef void (OXML::* ENDTAG)(const char *);

--
Any opinions expressed are my own and not necessarily those of Roundpoint.



Tue, 28 Jan 2003 03:00:00 GMT  
 Casting a method pointer to a function pointer.
Thank's Ben Hutching and Danny Kalev.
Quote:


[...]
> > How could I cast a method pointeur to a function pointer ?
> > ( I guess it is my problem ).
> <snip>
> Read what the compiler tells you - the conversion is not possible.

Yes, I have made a error.
static function doesn't work because my method must be virtual.
Quote:
> You must never cast function pointers or member function pointers.
> Instead, correct the BEGINTAG and ENDTAG types:
>     typedef void (OXML::* BEGINTAG)(const char *,const char **);
>     typedef void (OXML::* ENDTAG)(const char *);

I have the solution of my problem.
I don't tell you everything thing because I could't send you all ... of
course.

In the function I have to call there's a user data ( void * )
I put the object (this) in the userdata.
I define two functions for the callback of the C library.
And in I cast the userdata to the objet and I call the method.

I guess it's a wrapper C to C++, or something like that.

And it work, and It's more clean ... ???

charles vidal.



Fri, 31 Jan 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to use (pointer to function), and function and pointer to (pointer to function)

2. Casting function pointer to void pointer

3. casting void pointer to be a pointer to a function

4. casting void pointer to be a pointer to a function

5. Pointer Functions and Pointers to Pointer Functions

6. QUESTION: Casting a Pointer to 1 of Many Possible Pointers

7. cast pointer to struct to pointer to type of first member of struct

8. can I cast pointer to pointers

9. can I cast pointer to pointers?

10. Type Casting Const Pointers to non const pointers

11. Question about signal()/pointers to functions that return pointers to functions

12. function pointers and function pointers array

 

 
Powered by phpBB® Forum Software