WW wrote:
vw at iep dot tu-graz dot ac dot at" <"vw at iep dot tu-graz dot ac dot
at wrote:
Hello
Is there something more elegant in C++ to read/write data
binary into a stream than for example:
out.write(reinterpret_cast<const char *>(&long_var),
sizeof(long_var)); in.read(reinterpret_cast<char *>(&unsigned_var),
sizeof(unsigned_var));
The need for reinterpret_cast<> looks suspicious.
Yes. Use static_cast. *Never* use reinterpret_cast, unless you are doing
something deliberately non-portable.
You can't static_cast from long* to const char* directly, though. You'd
have to use double static_casts, right?
static_cast<const char *>(static_cast<void *>(&long_var))
Or I suppose you could define a new type of cast:
template<class D, class S>
D safe_reinterpret(const S &source)
{
return static_cast<D>(static_cast<void *>(source));
}
OK, that's rather ugly, and not very safe at all, but it might be a good
solution with some more work.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.