casting ostream

L

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'

is there anyway to make this cast? thanks!
 
D

Dave Rahardja

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
 
A

Alf P. Steinbach

* 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.
 
J

Jim Langston

Dave Rahardja said:
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.
 
I

I V

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:

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.
 
D

Dave Rahardja

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
 
G

Greg

Dave said:
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
 
D

Dave Rahardja

...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!
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top