C
coal
I've been thinking about how to build(receive) an object from a stream
if the object's type has a const data member. While it would be great
to have ctors that read from a stream, I think const data members are
a difficult obstacle to making progress in that direction. Another
option would be to use an ordinary function called Receive. Say we
have this class:
class Cd {
int const az_;
public:
int Send(Buffer*, int = 0) const;
int Receive(Buffer*);
};
The code I'm thinking about using with Receive is:
int
Cd::Receive(Buffer* buf)
{
if (!buf->Give(const_cast<int*>(&az_), sizeof(int))) {
buf->ews_.SetErrorWords(1, __FILE__, __LINE__);
return 0;
}
return 1;
}
(*Buffer is here http://home.seventy7.com/misc/Buffer.hh.)
That seems fine to me. However, in this thread, http://preview.tinyurl.com/2yw4aa,
Eugene Gershnik wrote,
"No this is not what I have meant. What I did mean was that if you
need to interoprate with const-less library that you know
won't change the data you can cast away const on the boundary....
This and implementing non-const function in terms of const one are the
only acceptable uses of const_cast."
(It helps if you put a comma after the word data.)
I guess the const_cast above wouldn't be what he considers an
acceptable use. Is that rule commonly held? Any ideas on how to
improve the implementation?
Thanks.
Brian Wood
Ebenezer Enterprises
www.webebenezer.net
If you are in the United States illegally, you need to leave.
if the object's type has a const data member. While it would be great
to have ctors that read from a stream, I think const data members are
a difficult obstacle to making progress in that direction. Another
option would be to use an ordinary function called Receive. Say we
have this class:
class Cd {
int const az_;
public:
int Send(Buffer*, int = 0) const;
int Receive(Buffer*);
};
The code I'm thinking about using with Receive is:
int
Cd::Receive(Buffer* buf)
{
if (!buf->Give(const_cast<int*>(&az_), sizeof(int))) {
buf->ews_.SetErrorWords(1, __FILE__, __LINE__);
return 0;
}
return 1;
}
(*Buffer is here http://home.seventy7.com/misc/Buffer.hh.)
That seems fine to me. However, in this thread, http://preview.tinyurl.com/2yw4aa,
Eugene Gershnik wrote,
"No this is not what I have meant. What I did mean was that if you
need to interoprate with const-less library that you know
won't change the data you can cast away const on the boundary....
This and implementing non-const function in terms of const one are the
only acceptable uses of const_cast."
(It helps if you put a comma after the word data.)
I guess the const_cast above wouldn't be what he considers an
acceptable use. Is that rule commonly held? Any ideas on how to
improve the implementation?
Thanks.
Brian Wood
Ebenezer Enterprises
www.webebenezer.net
If you are in the United States illegally, you need to leave.