casting ostream

Discussion in 'C++' started by laniik, Jan 21, 2007.

  1. laniik

    laniik Guest

    Hi, I am trying to understand some existing code involving casting
    std::eek:stream (which supposedly compiles, but doesn't seem to on my
    compiler)

    basically, i want to pass a std::eek:stream as a void* arguemnt

    i have a fn:

    bool callback(float percentComplete, void* stream)
    {
    ((ofstream)(&stream))<< percentComplete << "% complete" << endl;
    return true;
    }

    and im using it:

    solver.setCompletionCallback(&callback, &cout);

    using visual studio .net 2005, i get an error

    'type cast' : cannot convert from 'void *' to 'std::eek:fstream'

    is there anyway to make this cast? thanks!
     
    laniik, Jan 21, 2007
    #1
    1. Advertising

  2. On 20 Jan 2007 16:39:28 -0800, "laniik" <> wrote:

    >Hi, I am trying to understand some existing code involving casting
    >std::eek:stream (which supposedly compiles, but doesn't seem to on my
    >compiler)
    >
    >basically, i want to pass a std::eek:stream as a void* arguemnt
    >
    >i have a fn:
    >
    >bool callback(float percentComplete, void* stream)
    >{
    > ((ofstream)(&stream))<< percentComplete << "% complete" << endl;
    > return true;
    >}
    >
    >and im using it:
    >
    >solver.setCompletionCallback(&callback, &cout);
    >
    >using visual studio .net 2005, i get an error
    >
    >'type cast' : cannot convert from 'void *' to 'std::eek:fstream'
    >
    >is there anyway to make this cast? thanks!


    bool callback(float percentComplete, void* stream)
    {
    *reinterpret_cast<ofstream*>(stream) << percentComplete
    << "% complete" << endl;
    return true;
    }

    I hope you know what you're doing. reinterpret_cast's are red flags unless
    you're performing very low level (system) operations.

    -dr
     
    Dave Rahardja, Jan 21, 2007
    #2
    1. Advertising

  3. * laniik:
    > Hi, I am trying to understand some existing code involving casting
    > std::eek:stream (which supposedly compiles, but doesn't seem to on my
    > compiler)
    >
    > basically, i want to pass a std::eek:stream as a void* arguemnt
    >
    > i have a fn:
    >
    > bool callback(float percentComplete, void* stream)
    > {
    > ((ofstream)(&stream))<< percentComplete << "% complete" << endl;
    > return true;
    > }
    >
    > and im using it:
    >
    > solver.setCompletionCallback(&callback, &cout);
    >
    > using visual studio .net 2005, i get an error
    >
    > 'type cast' : cannot convert from 'void *' to 'std::eek:fstream'


    This error message does not correspond to the code shown above.


    > is there anyway to make this cast? thanks!


    Use types, avoid casts.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 21, 2007
    #3
  4. laniik

    Jim Langston Guest

    "Dave Rahardja" <> wrote in message
    news:...
    > On 20 Jan 2007 16:39:28 -0800, "laniik" <> wrote:
    >
    >>Hi, I am trying to understand some existing code involving casting
    >>std::eek:stream (which supposedly compiles, but doesn't seem to on my
    >>compiler)
    >>
    >>basically, i want to pass a std::eek:stream as a void* arguemnt
    >>
    >>i have a fn:
    >>
    >>bool callback(float percentComplete, void* stream)
    >>{
    >> ((ofstream)(&stream))<< percentComplete << "% complete" << endl;
    >> return true;
    >>}
    >>
    >>and im using it:
    >>
    >>solver.setCompletionCallback(&callback, &cout);
    >>
    >>using visual studio .net 2005, i get an error
    >>
    >>'type cast' : cannot convert from 'void *' to 'std::eek:fstream'
    >>
    >>is there anyway to make this cast? thanks!

    >
    > bool callback(float percentComplete, void* stream)
    > {
    > *reinterpret_cast<ofstream*>(stream) << percentComplete
    > << "% complete" << endl;
    > return true;
    > }
    >
    > I hope you know what you're doing. reinterpret_cast's are red flags unless
    > you're performing very low level (system) operations.


    He seems to be using an engine of some type that calls a callback function
    that allows an interger and as a catch all a void* for whatever the user
    wants to use it for. This is fairly typical of engines that are made in
    windows using .dlls. The dll knows nothing about his code, nor can it.
    It's unfortunate, and I wish someone woudl come up with a good alternative
    these engine creators could use.
     
    Jim Langston, Jan 21, 2007
    #4
  5. laniik

    I V Guest

    On Sat, 20 Jan 2007 16:39:28 -0800, laniik wrote:

    > Hi, I am trying to understand some existing code involving casting
    > std::eek:stream (which supposedly compiles, but doesn't seem to on my
    > compiler)
    >
    > basically, i want to pass a std::eek:stream as a void* arguemnt


    Passing a std::eek:fstream as a void* almost certainly doesn't make sense.
    What might work (and what you actually do in the code below) is passing a
    std::eek:fstream* (that is, a pointer, not the ofstream itself).

    > i have a fn:
    >
    > bool callback(float percentComplete, void* stream)
    > {
    > ((ofstream)(&stream))<< percentComplete << "% complete" << endl;
    > return true;
    > }


    Note that here you are taking the address of the void* and trying to cast
    it to an ofstream - this isn't going to work. What you want to do is cast
    the void* to an ofstream* and dereference that to use the ofstream. Like:

    (*static_vast<ofstream*>(stream))<< percentComplete << "% complete"
    << endl;

    > solver.setCompletionCallback(&callback, &cout);


    Like I say, what you are doing here is passing a pointer to the ofstream,
    not the ofstream itself; which is good, and should work with my code above.
     
    I V, Jan 21, 2007
    #5
  6. On Sat, 20 Jan 2007 18:36:54 -0800, "Jim Langston" <>
    wrote:


    >> I hope you know what you're doing. reinterpret_cast's are red flags unless
    >> you're performing very low level (system) operations.

    >
    >He seems to be using an engine of some type that calls a callback function
    >that allows an interger and as a catch all a void* for whatever the user
    >wants to use it for. This is fairly typical of engines that are made in
    >windows using .dlls. The dll knows nothing about his code, nor can it.
    >It's unfortunate, and I wish someone woudl come up with a good alternative
    >these engine creators could use.
    >


    Yes. In such cases I would recommend that the OP write a proxy layer that
    restores the types before it calls the application code, e.g.


    namespace App
    {

    void callback(float percentComplete, ostream& stream);

    }

    namespace DLLProxy
    {

    void lowLevelCallback(float percentComplete, void* param)
    {
    ::App::callback(floaw, *reinterpret_cast<ostream*>(param));
    }

    }

    -dr
     
    Dave Rahardja, Jan 21, 2007
    #6
  7. laniik

    Greg Guest

    Dave Rahardja wrote:
    > On 20 Jan 2007 16:39:28 -0800, "laniik" <> wrote:
    > >i have a fn:
    > >
    > >bool callback(float percentComplete, void* stream)
    > >{
    > > ((ofstream)(&stream))<< percentComplete << "% complete" << endl;
    > > return true;
    > >}
    > >
    > >and im using it:
    > >
    > >solver.setCompletionCallback(&callback, &cout);
    > >
    > >using visual studio .net 2005, i get an error
    > >'type cast' : cannot convert from 'void *' to 'std::eek:fstream'
    > >
    > >is there anyway to make this cast? thanks!

    >
    > bool callback(float percentComplete, void* stream)
    > {
    > *reinterpret_cast<ofstream*>(stream) << percentComplete
    > << "% complete" << endl;
    > return true;
    > }
    >
    > I hope you know what you're doing. reinterpret_cast's are red flags unless
    > you're performing very low level (system) operations.


    ....which is the reason why a static_cast would be more appropriate in
    this situation:

    bool callback(float percentComplete, void* stream)
    {
    assert( stream != NULL );

    ofstream * s = static_cast<ofstream*>(stream);

    *s << percentComplete << "% complete" << endl;

    return true;
    }

    Greg
     
    Greg, Jan 21, 2007
    #7
  8. On 21 Jan 2007 08:27:19 -0800, "Greg" <> wrote:


    >> I hope you know what you're doing. reinterpret_cast's are red flags unless
    >> you're performing very low level (system) operations.

    >
    >...which is the reason why a static_cast would be more appropriate in
    >this situation:
    >
    > bool callback(float percentComplete, void* stream)
    > {
    > assert( stream != NULL );
    >
    > ofstream * s = static_cast<ofstream*>(stream);
    >
    > *s << percentComplete << "% complete" << endl;
    >
    > return true;
    > }


    Duh, you're right of course!
     
    Dave Rahardja, Jan 22, 2007
    #8
    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. kevin
    Replies:
    11
    Views:
    5,834
    Andrew McDonagh
    Jan 8, 2005
  2. Alex Dribin

    Line wrapping in ostream

    Alex Dribin, Aug 6, 2003, in forum: C++
    Replies:
    2
    Views:
    370
    Dietmar Kuehl
    Aug 6, 2003
  3. Wally Barnes
    Replies:
    3
    Views:
    540
    Wally Barnes
    Nov 20, 2008
  4. Replies:
    2
    Views:
    1,947
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    587
    James Kanze
    Dec 20, 2009
Loading...

Share This Page