 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mike P. Wagner Guest
|
Posted: Fri Jun 27, 2003 7:13 pm Post subject: const functions and IO |
|
|
I have a real world problem that I hope someone has solved. I
implementing a class in a kernel that stores (and reads) data
persistently. There is one underlying "raw IO" function that calls the
device. This function cannot be const (unless I lie) since it changes
persistent data.
But that means that none of my read functions can be const, since they
need to call the underlying "raw IO" function. Now that seems really
silly.
Is there a simple way to tell the compiler, "Look, when I call this
function with read requests, it's const"?
I know that I can goof around with typedefs and reinterpret_cast<> to
do this, but it seems like there must be a more elegant way to do
this.
It's not an exotic problem, is it?
Mike
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Fri Jun 27, 2003 7:28 pm Post subject: Re: const functions and IO |
|
|
"Mike P. Wagner" <mikepwagner (AT) hotmail (DOT) com> wrote
| Quote: | I have a real world problem that I hope someone has solved. I
implementing a class in a kernel that stores (and reads) data
persistently. There is one underlying "raw IO" function that calls the
device. This function cannot be const (unless I lie) since it changes
persistent data.
But that means that none of my read functions can be const, since they
need to call the underlying "raw IO" function. Now that seems really
silly.
Is there a simple way to tell the compiler, "Look, when I call this
function with read requests, it's const"?
I know that I can goof around with typedefs and reinterpret_cast<> to
do this, but it seems like there must be a more elegant way to do
this.
It's not an exotic problem, is it?
Mike
|
Declare your persistent data to be mutable and declare the rawIO function
const. Even const functions are allowed to change mutable data
class X
{
mutable int data;
};
mutable is useful on caches and the like, which if I understand you
correctly is what you have.
john
|
|
| Back to top |
|
 |
Thomas Matthews Guest
|
Posted: Fri Jun 27, 2003 7:28 pm Post subject: Re: const functions and IO |
|
|
Mike P. Wagner wrote:
| Quote: | I have a real world problem that I hope someone has solved. I
implementing a class in a kernel that stores (and reads) data
persistently. There is one underlying "raw IO" function that calls the
device. This function cannot be const (unless I lie) since it changes
persistent data.
But that means that none of my read functions can be const, since they
need to call the underlying "raw IO" function. Now that seems really
silly.
Is there a simple way to tell the compiler, "Look, when I call this
function with read requests, it's const"?
I know that I can goof around with typedefs and reinterpret_cast<> to
do this, but it seems like there must be a more elegant way to do
this.
It's not an exotic problem, is it?
Mike
|
Most of the time, reading is not const and destroys things.
If you want to make member function const and have it modify
member variables, declare those variables as "mutable".
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|