cast question

Discussion in 'C++' started by Patrick Guio, Dec 6, 2004.

  1. Patrick Guio

    Patrick Guio Guest

    Dear all,


    I have a C function that returns a void pointer (void *).
    This object pointer is actually a pointer to function.
    I have the following declarations:

    void *pobj;
    double (*func)(double);

    I am trying to convert pobj into func with a reinterpret_cast

    fun = reinterpret_cast<double (*)(double)>(pobj);

    but I get a compiler error that says that ISO C++ forbids casting between
    pointer-to-function and pointer-to-object.

    Any idea how to force such cast ?

    Sincerely,

    Patrick
    Patrick Guio, Dec 6, 2004
    #1
    1. Advertising

  2. Patrick Guio wrote:

    > I have a C function that returns a void pointer (void *).
    > This object pointer is actually a pointer to function.
    > I have the following declarations:
    >
    > void *pobj;
    > double (*func)(double);
    >
    > I am trying to convert pobj into func with a reinterpret_cast
    >
    > fun = reinterpret_cast<double (*)(double)>(pobj);
    >
    > but I get a compiler error that says that ISO C++ forbids casting
    > between pointer-to-function and pointer-to-object.
    >
    > Any idea how to force such cast ?



    By using a C style cast:


    func=(double (*) (double) ) (pobj);




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Dec 6, 2004
    #2
    1. Advertising

  3. Patrick Guio

    Siemel Naran Guest

    "Patrick Guio" <> wrote in message

    > I am trying to convert pobj into func with a reinterpret_cast
    >
    > fun = reinterpret_cast<double (*)(double)>(pobj);
    >
    > but I get a compiler error that says that ISO C++ forbids casting between
    > pointer-to-function and pointer-to-object.


    My compiler (Borland 6) allows the cast, though another (g++ 2.95) forbids
    it. I guess g++ is right.

    However, you can reinterpret_cast from one function type to another, which
    might be be of use.
    Siemel Naran, Dec 6, 2004
    #3
  4. Patrick Guio

    Serock Guest

    You can convert pobj into func as below:

    void *pobj;
    double (*func)(double);
    *((void**)&func) = pobj;

    under g++ 2.4.2
    Serock, Dec 6, 2004
    #4
  5. Patrick Guio

    Serock Guest

    You can try to convert pobj into func as below:

    void *pobj;
    double (*func)(double);
    *((void**)&func) = pobj;

    can pass under g++ 3.4.2
    Serock, Dec 6, 2004
    #5
  6. Patrick Guio

    Jack Klein Guest

    On Mon, 6 Dec 2004 10:31:20 +0100, Patrick Guio <>
    wrote in comp.lang.c++:

    >
    > Dear all,
    >
    >
    > I have a C function that returns a void pointer (void *).
    > This object pointer is actually a pointer to function.
    > I have the following declarations:
    >
    > void *pobj;
    > double (*func)(double);
    >
    > I am trying to convert pobj into func with a reinterpret_cast
    >
    > fun = reinterpret_cast<double (*)(double)>(pobj);
    >
    > but I get a compiler error that says that ISO C++ forbids casting between
    > pointer-to-function and pointer-to-object.
    >
    > Any idea how to force such cast ?
    >
    > Sincerely,
    >
    > Patrick


    There is no defined conversion between any pointer to object type,
    including pointer to void, and any pointer to function type. A
    C-style cast is accepted, but the result is completely undefined.

    Complain to the source of the function, it is, has been, and always
    will be bad code.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Dec 7, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Question: Invalid Cast Exception Error

    VB Programmer, Oct 28, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    1,351
    VB Programmer
    Oct 28, 2003
  2. Christopher Benson-Manica

    Quick cast style question

    Christopher Benson-Manica, Apr 30, 2004, in forum: C++
    Replies:
    15
    Views:
    532
    Jake Montgomery
    May 4, 2004
  3. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,072
    Dan Pop
    Feb 10, 2004
  4. EvilRix
    Replies:
    8
    Views:
    635
    Martin Dickopp
    Feb 14, 2004
  5. Pavel
    Replies:
    7
    Views:
    524
    Pavel
    Sep 19, 2010
Loading...

Share This Page