const functions and IO

M

Mike P. Wagner

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
 
J

John Harrison

Mike P. Wagner said:
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
 
T

Thomas Matthews

Mike said:
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
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top